JavaScript string concatenation speed -


can explain 1 me:

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

if you're lazy, tested a) vs b):

a)

var innerhtml = "";  items.foreach(function(item) {     innerhtml += item; }); 

b)

var innerhtml = items.join(""); 

where items both tests same 500-element array of strings, each string being random , between 100 , 400 characters in length.

a) ends being 10x faster. how can be--i thought concatenating using join("") optimization trick. there flawed tests?

using join("") optimization trick composing large strings on ie6 avoid o(n**2) buffer copies. never expected huge performance win composing small strings since o(n**2) dominates overhead of array largish n.

modern interpreters around using "dependent strings". see mozilla bug explanation of dependent strings , of advantages , drawbacks.

basically, modern interpreters knows number of different kinds of strings:

  1. an array of characters
  2. a slice (substring) of string
  3. a concatenation of 2 other strings

this makes concatenation , substring o(1) @ cost of keeping of substringed buffer alive resulting in inefficiency or complexity in garbage collector.

some modern interpreters have played around idea of further decomposing (1) byte[]s ascii strings, , arrays of uint16s when string contains utf-16 code unit can't fit 1 byte. don't know if idea in interpreter.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -