String concatenation (v62)

Revision 62 of this benchmark created on


Description

Different ways to concatenate strings together

Preparation HTML

<script>
  var arr = ['a', 'b', 'c', 'd']
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Direct concatenation
var foo = 'a' + 'b' + 'c' + 'd';
ready
Individual += statements
var foo = 'a';
foo += 'b';
foo += 'c';
foo += 'd';
ready
Individual statements
var foo = 'a';
foo = foo + 'b';
foo = foo + 'c';
foo = foo + 'd';
ready
Using Array#join
var foo = arr.join('');
ready
Single individual statement
var foo = 'a';
foo += 'b' + 'c' + 'd';
ready
Function based (for)
function concat(arr) {
  len = arr.length;
  for (s = "", i = 0; i < len; s += arr[i], i++);
  return s;
}

var foo = concat(arr);
ready
Simple For Loop
var foo = "";
for (i = 0; i < arr.length; ++i) {
    foo = foo + arr[i];
}
ready
Functional Concat
function concat(l) {
    if (l.length > 0) {
        return concat(l.slice(1)) + l[0];
    } else {
        return "";
    }
}

concat(arr);
ready
While Loop
var foo = "";
var i = arr.length;
while (i) {
    --i;
    foo = arr[i] + foo;
}
ready

Revisions

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