String vs Array concat (v2)

Revision 2 of this benchmark created on


Setup

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);
    }
  }

Test runner

Ready to run.

Testing in
TestOps/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);
  }
}
ready
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);
  }
}
ready
String w/o flattening by index (IE > 7)
var dest = 'apple';
while (dest.length < 100) {
  var offs = dest.length - 5;
  for (var i = offs; i < offs + 10; i++) {
    dest += precompiled[i];
  }
}
ready
Array
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('');
ready

Revisions

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