string concat vs array join

JavaScript performance comparison

Revision 46 of this test case created by m93a

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var words = ['this', 'is', 'a', 'test'];
  
  var counter = 0;
  var length = 16;
  var i;
  
  var re = /z/;
  var result;

};
</script>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Concat x += ''
var text = '';
for(i = 0; i < length; ++i)
{
    text += ' ' + words[i & 3] + (counter++);
}

result = re.test(text);
pending…
Concat x += '', separate substrings
var text = '';
for(i = 0; i < length; ++i)
{
    text += ' ';
    text += words[i & 3]
    text += counter++;
}

result = re.test(text);
pending…
Concat x = x + ''
var text = '';
for(i = 0; i < length; ++i)
{
    text = text + ' ' + words[i & 3] + (counter++);
}

result = re.test(text);
pending…
Concat x = x + '', separate substrings
var text = '';
for(i = 0; i < length; ++i)
{
    text = text + ' ';
    text = text + words[i & 3];
    text = text + (counter++);
}

result = re.test(text);
pending…
String.prototype.concat
var text = '';
for(i = 0; i < length; ++i)
{
    text = text.concat(' ', words[i & 3], (counter++));
}

result = re.test(text);
pending…
String.prototype.concat, +'d substring
var text = '';
for(i = 0; i < length; ++i)
{
    text = text.concat(' ' + words[i & 3] + (counter++));
}

result = re.test(text);
pending…
String.prototype.concat, separate substrings
var text = '';
for(i = 0; i < length; ++i)
{
    text = text.concat(' ');
    text = text.concat(""+words[i & 3]);
    text = text.concat(""+counter++);
}

result = re.test(text);
pending…
Array.push, multiple args
var a = [];
for(i = 0; i < length; ++i)
{
    a.push(' ', words[i & 3], counter++);
}

result = re.test(a.join(''));
pending…
Array.push, multiple calls
var a = [];
for(i = 0; i < length; ++i)
{
    a.push(' ');
    a.push(words[i & 3])
    a.push(counter++);
}

result = re.test(a.join(''));
pending…
Array.push, +'d arg
var a = [];
for(i = 0; i < length; ++i)
{
    a.push(' ' + words[i & 3] + (counter++));
}

result = re.test(a.join(''));
pending…
Array assignment
var a = [];
var j = 0;
for(i = 0; i < length; ++i)
{
    a[j++] = ' ';
    a[j++] = words[i & 3];
    a[j++] = counter++;
}

result = re.test(a.join(''));
pending…
Array assignment, +'d value
var a = [];
for(i = 0; i < length; ++i)
{
    a[i] = ' ' + words[i & 3] + (counter++);
}

result = re.test(a.join(''));
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.

0 Comments