Append string vs join array

JavaScript performance comparison

Revision 6 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var i = 0,
        str = '',
        arr = [],
        w1 = 'brown',
        w2 = 'jumped',
        w3 = 'lazy';
};
</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
String append #1
str = '';
for (j = 0; j < 1000; j++) {
  str += 'Quick ' + w1 + ' fox ' + w2 + ' over the ' + w3 + ' dog. ';
}
pending…
String append #2
str = '';
for (j = 0; j < 1000; j++) {
  str += 'Quick ';
  str += w1;
  str += ' fox ';
  str += w2;
  str += ' over the ';
  str += w3;
  str += ' dog. ';
}
pending…
Array join #1
arr = [];
arr2 = ['Quick ', w1, ' fox ', w2, ' over the ', w3, ' dog. '];
for (j = 0; j < 1000; j++) {
  arr.push.apply(arr, arr2);
}
arr.join('');
pending…
Array join #2
arr = [];
for (i = 0; i < 7000; i++) {
  arr[i++] = 'Quick ';
  arr[i++] = w1;
  arr[i++] = ' fox ';
  arr[i++] = w2;
  arr[i++] = ' over the ';
  arr[i++] = w3;
  arr[i++] = ' dog. ';
}
arr.join('');
pending…
String append #3
str = '';
for (j = 0; j < 1000; j++) {
  str.concat('Quick ', w1, ' fox ', w2, ' over the ', w3, ' dog. ');
}
pending…
String append #4
str = '';
for (j = 0; j < 1000; j++) {
  str.concat('Quick ');
  str.concat(w1);
  str.concat(' fox ');
  str.concat(w2);
  str.concat(' over the ');
  str.concat(w3);
  str.concat(' dog. ');
}
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