JavaScript string concatenation speed

Can someone explain this to me:

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

If you are lazy, I tested A) against B):

A)

var innerHTML = "";

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

B)

var innerHTML = items.join("");

Where itemsfor both tests is the same 500-element array of strings, each row being random and 100 to 400 characters long.

A) ends 10 times faster. How it could be - I always thought concatenating with join("")was an optimization trick. Is there anything missing in my tests?

+5
source share
2 answers

join("") IE6, O(n**2). , , O(n**2) n.

, " ". mozilla bug .

, :

  • Slice ()

O (1) , .

(1) [] s ASCII uint16s, UTF-16 , , , .

+8

Lua , @Mike Samuel. Lua, JavaScript .

+1

All Articles