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

Revision 45 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 length = 1500;
  var html = [];
  var str = "";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
+=
str = "";
while (str.length < length) {
  str += '0';
}
ready
push
str = "";
while (html.length < length) {
  html.push('0');
}
str = html.join("");
ready
unshift
str = "";
while (html.length < length) {
  html.unshift('0');
}
str = html.join("");
ready
push reverse
str = "";
while (html.length < length) {
  html.push('0');
}
html.reverse();
str = html.join("");
ready
unshift reverse
str = "";
while (html.length < length) {
  html.unshift('0');
}
html.reverse();
str = html.join("");
ready
fast push
str = "";
while (html.length < length) {
  html[html.length] = "0";
}
str = html.join("");
ready
fast push reverse
str = "";
while (html.length < length) {
  html[html.length] = "0";
}
html.reverse();
str = html.join("");
ready
= + +
str = "";
while (str.length < length) {
  str = str + ',' + '0';
}
ready

Revisions

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