string concatenation

JavaScript performance comparison

Revision 6 of this test case created by Franz Josef Kaiser

Info

checks different string concatenation methods

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var s, S, A, X, Y, concat = String.prototype.concat;
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
regular concatenation
s = '';
s += 'aa';
s += 'bb';
s += 'cc';
s += 'dd';
s += 'ee';
s += 'ff';
s += 'gg';
s += 'hh';
s += 'ii';
pending…
array join concatenation
S = [];
S.push('aa');
S.push('bb');
S.push('cc');
S.push('dd');
S.push('ee');
S.push('ff');
S.push('gg');
S.push('hh');
S.push('ii');
S = S.join('');
pending…
array join concatenation (all at once)
X = [
'aa',
'bb',
'cc',
'dd',
'ee',
'ff',
'gg',
'hh',
'ii'
].join('');
pending…
array init and set before join
A = new Array(9);
A[0] = 'aa';
A[1] = 'bb';
A[2] = 'cc';
A[3] = 'dd';
A[4] = 'ee';
A[5] = 'ff';
A[6] = 'gg';
A[7] = 'hh';
A[8] = 'ii';
A = A.join('');
pending…
similar to 3 but using new Array
Y = (new Array(
'aa',
'bb',
'cc',
'dd',
'ee',
'ff',
'gg',
'hh',
'ii'
)).join('');
pending…
String.prototype.concat
concat.call(
'aa',
'bb',
'cc',
'dd',
'ee',
'ff',
'gg',
'hh',
'ii');
pending…
''.concat
'aa'.concat(
'bb',
'cc',
'dd',
'ee',
'ff',
'gg',
'hh',
'ii');
pending…
Regular concat, one line
s = '' + 'aa' + 'bb' + 'cc' + 'dd' + 'ee' + 'ff' + 'gg' + 'hh' + 'ii';
pending…
Slash concat
'aa\
bb\
cc\
dd\
ee\
ff\
gg\
hh\
ii'
;
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment