String.concat() vs. Array .join() vs. + operator (v12)

Revision 12 of this benchmark created by smITc on


Description

This testcase compares performance of string concatenation (+) versus the native Array.prototype.join() and String.prototype.concat() methods.

Preparation HTML

<script>
  var array = [];
  for (i = 0; i < 500; ++i)
  array.push("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" + i);
//corrected for undefined array members and this[0]===0
  Array.prototype._join = function(p) {
    var l = this.length,
        i = 0,
        r = this[i],
        s = p || '',
        undefined;
        r = r===undefined ? '' : r;
    while (++i < l) r += s + (this[i]===undefined?'':this[i]);
    return r;
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Array._join
var result = array._join('-');
ready
Array.join
var result = array.join('-');
ready
while loop
var l = array.length,
    r = array[0] || "",
    i = 1,
    s = '-';
while (i < l) r += s + array[i++];
ready
closure function
var r = (function(l, s) {
  var x = array[0] || '',
      i = 1;
  while (i < l) x += s + array[i++];
  return x;
})(array.length, '-');
ready

Revisions

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