String concatenation

JavaScript performance comparison

Revision 8 of this test case created

Info

Different ways to concatenate strings together

Preparation code

<script>
  var arr = ['a', 'b', 'c', 'd']
</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
Direct concatenation
var foo = 'a' + 'b' + 'c' + 'd';
pending…
Individual += statements
var foo = 'a';
foo += 'b';
foo += 'c';
foo += 'd';
pending…
Individual statements
var foo = 'a';
foo = foo + 'b';
foo = foo + 'c';
foo = foo + 'd';
pending…
Using Array#join
var foo = arr.join('');
pending…
Single individual statement
var foo = '';
foo += 'a' + 'b' + 'c' + 'd';
pending…
Using "\" to wrap string (no concat)
var foo = "a \
b \
c \
d"
;
pending…
template-style replacement
var foo = "1 2 3 4";
foo = foo.replace(/\d/g, function(match) {
 return arr[match];
});
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:

2 comments

Tyson Cadenhead commented :

Using "\" might be faster, but it won't validate if you run it through JSLint. Using that method would definitely raise concerns about future browser compatibility.

vamp commented :

Error in last expression (use arr[match-1] instead arr[match])

Add a comment