1

Some time ago declarations like the following were defined by poor performance:

var a = "my" +
"very" +
"very" +
"long" +
"string" +
"and" + 
"even" +
"longer";

I was told that every subsequent + operation causes an additional string to be created since they are immutable. At least, once that was an issue in Java programming language (mind StringBuffer vs. String).

I am talking about the more-or-less recent versions of the browsers of course.

The question is about JavaScript now: is it still not recommended or the runtime can squack (or should I say optimize) the issue like one above in milliseconds without any performance overhead?

2
  • You can do your own js performance tests on jsperf.com. For example jsperf.com/string-concat-js which seems to indicate that it doesn't matter with modern browsers. Commented Nov 16, 2011 at 14:01
  • @James: did it, looks I am correct (it took a very short time), but still curious for the global wisdom answer. Commented Nov 16, 2011 at 14:04

3 Answers 3

2

This makes more sense if you're concatenating a large amount of string values:

str = [
    'string1',
    'string2',
    'string3',
    'string4',
    'etc.'
].join( '' );
Sign up to request clarification or add additional context in comments.

3 Comments

@BreakPhreak What do you mean?
in the lines (like in my example) where the object is declared at its first time (and not mutated afterwards). am I clear enough, please?
@BreakPhreak That doesn't make a difference. This var x; x = ...; is equivalent to this var x = ...;.
0

A number of points:

  • What you cite was never a performance problem in Java; the compiler always optimized it using StringBuffer. Problems only arise when appending to a variable in a loop.
  • Java and JavaScript have absolutely nothing to do with each other.
  • The JavaScript runtime would have to work very hard to de-optimize the issue until it takes milliseconds. On a modern CPU, a millisecond is an eternity.
  • Premature optimization. Don't.
  • Profile/Benchmark before you optimize. Here's numbers from someone who did, but note that the results are 3 years old and thus meaningless.

Comments

0

I just found a simple implementation of the StringBuffer behaviour using JavaScript:

function StringBuffer() {
   this.buffer = [];
}

StringBuffer.prototype.append = function append(string) {
   this.buffer.push(string);
   return this;
 };

 StringBuffer.prototype.toString = function toString() {
   return this.buffer.join("");
 };

 var buf = new StringBuffer();

 buf.append("hello");
 buf.append("world");

 alert(buf.toString());

Source: http://social.msdn.microsoft.com/Forums/en-US/iewebdevelopment/thread/72429b15-73e2-4b4e-9288-9e1dfd47858b/

1 Comment

Well, my question is more about the concatenation in declaration line. The optimisations there can be peanuts for the modern JS engines.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.