On 2009/10/03 06:00:41, Christian Plesner Hansen wrote:
> I think waiting to flatten until everything is done sounds reasonable.   
> Just
> note that when using StringAdd the string won't be flattened until the  
> first
> time it is used, whereas with the string builder the string is flattened  
> when
it
> is returned from concat.  If your benchmarks doesn't use the string after
> creating it (say by reading the last character) the results are misleading
since
> the cost won't include the flattening.

> On the other hand if concat'ing 20 strings together and reading the last
> character is faster with a StringAdd based concat than a StringBuilder  
> then I
> agree it's better to use StringAdd.

well, here's the benchmark I used:

var t1 = new Date().getTime();
lala = "hello";
for(var i = 0; i < 50000; i++) {
        lala = lala + "a" + "a" + "a" + "a" + "a" + "a" + "a" + "a" + "a" + "a" 
 
+ "a" +
"a" + "a" + "a" + "a" + "a" + "a" + "a" + "a" + "a";
        lala.charAt(lala.length-1);
}

print((new Date().getTime() - t1) + 'ms');

//------------

var t1 = new Date().getTime();
lala = "hello";
for(var i = 0; i < 50000; i++) {
        lala =  
lala.concat("a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a", "a",
"a", "a", "a", "a", "a", "a", "a", "a");
        lala.charAt(lala.length-1);
}

print((new Date().getTime() - t1) + 'ms');


this reveals:

(1 string - not shown)
134ms (baseline)
120ms (concat using StringAdd)
807ms (concat using StringBuilder)

(20 strings - shown)
74ms (baseline - is actually faster, because it's affected by my other  
pending
patch)
877ms (concat using StringAdd)
35201ms (concat using StringBuilder)

http://codereview.chromium.org/243053

--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to