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

Revision 25 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 = 500;
  var html = [];
  var str = "";
  var elem = "abacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwaeabacdashwgdeyhaisudhafywegfyuiwgauyfgwae";
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
+=
str = '';
for (var i = 0; i < length; i++) {
  str += elem + elem + elem + elem + elem + elem;
}
ready
push
str = '';
for (var i = 0; i < length; i++) {
  html.push([elem, elem, elem, elem, elem, elem].join(''));

}
str = html.join('');
ready
fast push
str = '';
for (var i = 0; i < length; i++) {
  html[html.length] = [elem, elem, elem, elem, elem, elem].join('');
}
str = html.join('');
ready
+= [list]
str = '';
for (var i = 0; i < length; i++) {
  str += [elem, elem, elem, elem, elem, elem].join('');
}
ready

Revisions

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