Giter VIP home page Giter VIP logo

Comments (17)

ljharb avatar ljharb commented on May 7, 2024 7

Nowaways it's preferred to use an ES6 template string literal - which transpiles to a long string (with or without breaks, i forget). This entire issue seems like a moot point now :-)

from javascript.

hshoff avatar hshoff commented on May 7, 2024

Hi Rafi, thanks for the checking out the style guide and including a jsPerf.

We think that if you are fine-tuning your app and you find that the bottleneck is string concatenation, then by all means move to a single line and leave a comment saying "this was slowing down the app, moved to a single line" kind of thing.

Ideally, we would avoid this mess all together and keep really long strings out of our apps, but sometimes it comes up.

This is one of those cases why we call it a "mostly reasonable approach to javascript", not everything was decided by a jsPerf, we just made a style call and went with clarity over performance.

My gut feeling is there will be many other things to optimize for performance before you get down to long string concatenation, but I haven't come across this problem before.

I'll leave this issue open for now and hopefully some other folks will chime in with an opinion and we can correct it if necessary.

🍻

from javascript.

reissbaker avatar reissbaker commented on May 7, 2024

It's a good point that it's slower, but I agree with @hshoff. If you have a rare case where it's a bottleneck, definitely do what it takes to get rid of it (and include a comment!), but in general we aim for readability over raw speed.

from javascript.

rjacoby avatar rjacoby commented on May 7, 2024

Totally agree with @hshoff and @reissbaker on this as an less-common bottleneck. Maybe just note that it shouldn't be overused?

Honestly it just popped out at me b/c of general concat performance in other languages (Java, Ruby).

from javascript.

hshoff avatar hshoff commented on May 7, 2024

@rjacoby good call. I'll make a note and add the jsPerf.

from javascript.

michaelmior avatar michaelmior commented on May 7, 2024

You really shouldn't have to worry about this in your code and this should be handled by whatever tool you use to minify your JS for production. For example, uglifyjs will turn

var x = "a" + "b";

into

var x="ab";

from javascript.

bridge-rabish avatar bridge-rabish commented on May 7, 2024

I used this method for long string

['<div>',
    '<p>this is test</p>',
'</div>'].join('')

from javascript.

gustavopaes avatar gustavopaes commented on May 7, 2024

Use join() it's a bad idea.
http://jsperf.com/ya-string-concat/10

from javascript.

Nemoden avatar Nemoden commented on May 7, 2024

So, why exactly strings concatenation is preferred by the Guide if it's 95% slower than joining it with the break symbol ()? It doesn't add much readability and extremely slow.

js_perf_long_strings

from javascript.

nkbt avatar nkbt commented on May 7, 2024

@Nemoden maybe because of readability. When string concatenation becomes a bottleneck in the app, then it can be easily converted to the fastest way available.

from javascript.

 avatar commented on May 7, 2024

Ok

from javascript.

yonghu86 avatar yonghu86 commented on May 7, 2024

well,it's not length.

from javascript.

michaelmior avatar michaelmior commented on May 7, 2024

I think concatenation is much more readable for the sole fact that you can keep the correct level of indentation. As I noted in my previous comment, I think concatenation of static strings should be removed by build tools so performance should not really ever be an issue.

from javascript.

ibc avatar ibc commented on May 7, 2024

I think concatenation is much more readable for the sole fact that you can keep the correct level of indentation.

Or you can use a proper editor that properly indents extra lines if they belong to the same line.

from javascript.

ivanjonas avatar ivanjonas commented on May 7, 2024

It seems that the latest versions of Chrome have made performance a moot point. Here's a screenshot of revision 6 results.
image

from javascript.

GrayedFox avatar GrayedFox commented on May 7, 2024

Not quite a moot point.... basically, referencing variables inside of a template literal is still much slower than string concatenation, however without referencing a variable (and simply using a template literal for line breaks) seems to have no performance impact... https://jsperf.com/es6-string-literals-vs-string-concatenation/4

from javascript.

ljharb avatar ljharb commented on May 7, 2024

But also, "much slower" won't make any difference unless you're building millions of strings per second. It's identically fast for most use cases.

from javascript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.