String concatenation (v20)

Revision 20 of this benchmark created on


Description

Different ways to concatenate strings together

Preparation HTML

<script>
  var arr = ['a', 'b', 'c', 'd','e','f','g','h','a', 'b', 'c', 'd','e','f','g','h','a', 'b', 'c', 'd','e','f','g','h','a', 'b', 'c', 'd','e','f','g','h','a', 'b', 'c', 'd','e','f','g','h','a', 'b', 'c', 'd','e','f','g','h','a', 'b', 'c', 'd','e','f','g','h','a', 'b', 'c', 'd','e','f','g','h']
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Direct concatenation
var foo = 'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' +  'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' +  'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' +  'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' + 'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' +  'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' +  'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' + 'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h';
ready
Individual += statements
var foo = 'a';
foo += 'b';
foo += 'c';
foo += 'd';
foo += 'e';
foo += 'f';
foo += 'g';
foo += 'h';
foo += 'a';
foo += 'b';
foo += 'c';
foo += 'd';
foo += 'e';
foo += 'f';
foo += 'g';
foo += 'h';
foo += 'a';
foo += 'b';
foo += 'c';
foo += 'd';
foo += 'e';
foo += 'f';
foo += 'g';
foo += 'h';
foo += 'a';
foo += 'b';
foo += 'c';
foo += 'd';
foo += 'e';
foo += 'f';
foo += 'g';
foo += 'h';
foo += 'a';
foo += 'b';
foo += 'c';
foo += 'd';
foo += 'e';
foo += 'f';
foo += 'g';
foo += 'h';
foo += 'a';
foo += 'b';
foo += 'c';
foo += 'd';
foo += 'e';
foo += 'f';
foo += 'g';
foo += 'h';
foo += 'a';
foo += 'b';
foo += 'c';
foo += 'd';
foo += 'e';
foo += 'f';
foo += 'g';
foo += 'h';
foo += 'a';
foo += 'b';
foo += 'c';
foo += 'd';
foo += 'e';
foo += 'f';
foo += 'g';
foo += 'h';
ready
Individual statements
var foo = 'a';
foo = foo + 'b';
foo = foo + 'c';
foo = foo + 'd';
foo = foo + 'e';
foo = foo + 'f';
foo = foo + 'g';
foo = foo + 'h';
foo = foo + 'a';
foo = foo + 'b';
foo = foo + 'c';
foo = foo + 'd';
foo = foo + 'e';
foo = foo + 'f';
foo = foo + 'g';
foo = foo + 'h';
foo = foo + 'a';
foo = foo + 'b';
foo = foo + 'c';
foo = foo + 'd';
foo = foo + 'e';
foo = foo + 'f';
foo = foo + 'g';
foo = foo + 'h';
foo = foo + 'a';
foo = foo + 'b';
foo = foo + 'c';
foo = foo + 'd';
foo = foo + 'e';
foo = foo + 'f';
foo = foo + 'g';
foo = foo + 'h';
foo = foo + 'a';
foo = foo + 'b';
foo = foo + 'c';
foo = foo + 'd';
foo = foo + 'e';
foo = foo + 'f';
foo = foo + 'g';
foo = foo + 'h';
foo = foo + 'a';
foo = foo + 'b';
foo = foo + 'c';
foo = foo + 'd';
foo = foo + 'e';
foo = foo + 'f';
foo = foo + 'g';
foo = foo + 'h';
foo = foo + 'a';
foo = foo + 'b';
foo = foo + 'c';
foo = foo + 'd';
foo = foo + 'e';
foo = foo + 'f';
foo = foo + 'g';
foo = foo + 'h';
ready
Using Array#join
var foo = arr.join('');
ready
Single individual statement
var foo = 'a';
foo += 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' + 'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' + 'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' + 'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' + 'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' + 'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' + 'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h' + 'a' + 'b' + 'c' + 'd' + 'e' + 'f' + 'g' + 'h';
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
no concatenation
var foo = "abcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefghabcdefgh"
ready
string.concat
"a".concat("b", "c", "d","e","f","g","h","a","b", "c", "d","e","f","g","h","a","b", "c", "d","e","f","g","h","a","b", "c", "d","e","f","g","h","a","b", "c", "d","e","f","g","h","a","b", "c", "d","e","f","g","h","a","b", "c", "d","e","f","g","h","a","b", "c", "d","e","f","g","h");
ready
string.concat array
String.prototype.concat.apply('', arr);
ready

Revisions

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