Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(21)

Issue 243053: Array.join() is really slow and has array memory allocation overhead... inste...

Created:
11 years, 2 months ago by kenny
Modified:
11 years, 2 months ago
CC:
v8-dev
Visibility:
Public.

Description

Array.join() is really slow and has array memory allocation overhead... instead concat the strings similar to str += "...";

Patch Set 1 #

Total comments: 1
Unified diffs Side-by-side diffs Delta from patch set Stats (+5 lines, -5 lines) Patch
M src/string.js View 1 chunk +5 lines, -5 lines 1 comment Download

Messages

Total messages: 4 (0 generated)
Christian Plesner Hansen
I think avoiding join is good since join comes with all sorts of overhead. However, ...
11 years, 2 months ago (2009-10-02 05:47:12 UTC) #1
kenny
hey thanks for the review! I actually thought about using the StringBuilder, but because of ...
11 years, 2 months ago (2009-10-02 22:11:58 UTC) #2
Christian Plesner Hansen
I think waiting to flatten until everything is done sounds reasonable. Just note that when ...
11 years, 2 months ago (2009-10-03 06:00:41 UTC) #3
kenny
11 years, 2 months ago (2009-10-03 07:30:28 UTC) #4
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)

Powered by Google App Engine
This is Rietveld 408576698