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

Revision 42 of this benchmark created 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 str = "";
for( var i= 0; i < count; ++i ){
  str += prefix + i;
}
ready
push
var buffer = Array(count);
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.