String vs Array concat

JavaScript performance comparison

Revision 16 of this test case created by greg

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  var precompiled = 'apple';
  while (precompiled.length < 100) {
    var offs = precompiled.length - 5;
    for (var i = offs; i < offs + 10; ++i) {
      // calling charAt(i) on `precompiled` will
      // flatten the string in most engines
      precompiled += precompiled.charAt(i);
    }
  }
  Array.prototype.fjoin=function(d){for(var a=0,c=this.length,b="";a<c;a++)b+=a<c-1?this[a]+d:this[a];return b};
  

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
String
var dest = 'apple';
while (dest.length < 100) {
  var offs = dest.length - 5;
  for (var i = offs; i < offs + 10; i++) {
    dest += dest.charAt(i);
  }
}
pending…
String w/o flattening
var dest = 'apple';
while (dest.length < 100) {
  var offs = dest.length - 5;
  for (var i = offs; i < offs + 10; i++) {
    dest += precompiled.charAt(i);
  }
}
pending…
fjoin
var dest = ['a', 'p', 'p', 'l', 'e'];
while (dest.length < 100) {
  var offs = dest.length - 5;
  for (var i = offs; i < offs + 10; i++) {
    dest[dest.length] = dest[i];
  }
}
dest = dest.fjoin('');
pending…
join
var dest = ['a', 'p', 'p', 'l', 'e'];
while (dest.length < 100) {
  var offs = dest.length - 5;
  for (var i = offs; i < offs + 10; i++) {
    dest[dest.length] = dest[i];
  }
}
dest = dest.join('');
pending…

Compare results of other browsers

Revisions

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

0 Comments