String concatenation: using += vs. using array push vs. unshift (v44)

Revision 44 of this benchmark created by Ger Hobbelt on


Description

Tests the performance difference of using two different ways of concatenating strings. The two TCs produce the same string result.

See http://jsperf.com/forloop-vs-reverse-while-loop-and-array-reverse/9 for array reverse vs custom loops.

See http://jsperf.com/array-push-reverse-vs-unshift-reverse for array push reverse vs. unshift reverse.

Preparation HTML

<script>
  var count= 1500;
  var prefix = 'qwertyuiopasdfghjklzxcvbnm'
  var buffer= [];
  var str = "";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
+=
var str2 = "";
for( var i= 0; i < count; ++i ){
  str2 += prefix + i;
}
str = str2;
ready
buffer
var buffer = Array(count);
for( var i= 0; i < count; ++i ){
  buffer[ i ] = prefix + i;
}
str = buffer.join("");
ready
push to buffer
var buffer = [];
for( var i= 0; i < count; ++i ){
  buffer.push( prefix + i );
}
str = buffer.join("");
ready

Revisions

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