String concatenation

JavaScript performance comparison

Revision 25 of this test case created by Hans

Info

Different ways to concatenate strings together

Preparation code

<script>
  var value = 4;
  var text = ["Privjyet", 4];
  var arr = ['<option value="', value, '">', text, '</option>']
</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 = '<option value="' + value + '">' + text + '</option>';
pending…
Individual += statements
var foo = '<option value="';
foo += value;
foo += '">';
foo += text;
foo += '</option>';
pending…
Individual statements
var foo = '<option value="';
foo = foo + value;
foo = foo + '">';
foo = foo + text;
foo = foo + '</option>';
pending…
Using Array#join
var foo = arr.join('');
pending…
Single individual statement
var foo = '<option value="';
foo += value + '">' + text + '</option>';
pending…
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);
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:

0 comments

Add a comment