string concat vs array join (v47)

Revision 47 of this benchmark created by Matt on


Setup

var words = ['this', 'is', 'a', 'test'];
  
  var counter = 0;
  var length = 16;
  var i;
  
  var re = /z/;
  var result;

Test runner

Ready to run.

Testing in
TestOps/sec
Concat x += ''
var text = '';
for(i = 0; i < length; ++i)
{
    text += ' ' + words[i & 3] + (counter++);
}

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

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

result = re.test(text);
ready
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);
ready
String.prototype.concat
var text = '';
for(i = 0; i < length; ++i)
{
    text = text.concat(' ', words[i & 3], (counter++));
}

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

result = re.test(text);
ready
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);
ready
Array.push, multiple args
var a = [];
for(i = 0; i < length; ++i)
{
    a.push(' ', words[i & 3], counter++);
}

result = re.test(a.join(''));
ready
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(''));
ready
Array.push, +'d arg
var a = [];
for(i = 0; i < length; ++i)
{
    a.push(' ' + words[i & 3] + (counter++));
}

result = re.test(a.join(''));
ready
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(''));
ready
Array assignment, +'d value
var a = [];
for(i = 0; i < length; ++i)
{
    a[i] = ' ' + words[i & 3] + (counter++);
}

result = re.test(a.join(''));
ready
Concat x += ", + delimiter on iterations > 0
var text = '';
for(i = 0; i < length; ++i)
{
    if(i > 0){
        text += ", ";
    }
    text += words[i & 3];
}
result = re.test(text);
ready
Array.push + delimiter
var a = [];
for(i = 0; i < length; ++i)
{
    a.push(words[i & 3])
}
result = re.test(a.join(", "));
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.