Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript string concatenation speed

Can someone explain this one to me:

http://jsperf.com/string-concatenation-1/2

If you're lazy, I tested A) vs B):

A)

var innerHTML = "";

items.forEach(function(item) {
    innerHTML += item;
});

B)

var innerHTML = items.join("");

Where items for both tests is the same 500-element array of strings, with each string being random and between 100 and 400 characters in length.

A) ends up being 10x faster. How can this be--I always thought concatenating using join("") was an optimization trick. Is there something flawed with my tests?

like image 707
FoobarisMaximus Avatar asked Jul 01 '11 18:07

FoobarisMaximus


People also ask

What is the time complexity of string concatenation JavaScript?

String concatenation It can be a bit surprising, but this code actually runs in O(N2) time. The reason is that in Java strings are immutable, and as a result, each time you append to the string new string object is created.

Is string concatenation slow?

Each time strcat calls, the loop will run from start to finish; the longer the string, the longer the loop runs. Until the string is extensive, the string addition takes place very heavy and slow.

What is the best way to concatenate strings in JavaScript?

The best and fastest way to concatenate strings in JavaScript is to use the + operator. You can also use the concat() method.

Is concatenation faster than join?

Doing N concatenations requires creating N new strings in the process. join() , on the other hand, only has to create a single string (the final result) and thus works much faster.


1 Answers

Using join("") was an optimization trick for composing large strings on IE6 to avoid O(n**2) buffer copies. It was never expected to be a huge performance win for composing small strings since the O(n**2) only really dominates the overhead of an array for largish n.

Modern interpreters get around this by using "dependent strings". See this mozilla bug for an explanation of dependent strings and some of the advantages and drawbacks.

Basically, modern interpreters knows about a number of different kinds of strings:

  1. An array of characters
  2. A slice (substring) of another string
  3. A concatenation of two other strings

This makes concatenation and substring O(1) at the cost of sometimes keeping too much of a substringed buffer alive resulting in inefficiency or complexity in the garbage collector.

Some modern interpreters have played around with the idea of further decomposing (1) into byte[]s for ASCII only strings, and arrays of uint16s when a string contains a UTF-16 code unit that can't fit into one byte. But I don't know if that idea is actually in any interpreter.

like image 118
Mike Samuel Avatar answered Sep 26 '22 00:09

Mike Samuel