string building from characters

JavaScript performance comparison

Test case created by RyanC

Preparation code

<script>
Benchmark.prototype.setup = function() {
  function randCharCode() {
    return Math.floor(Math.random()*95+32);
  }

  var len = 2048, c;

  var data_char = new Array(len);
  var data_code = new Array(len);
  var data_u8, data_u16;
  try {
    data_u8 = new Uint8Array(len);
  } catch(e) { data_u8 = data_code }
  try {
    data_u16 = new Uint16Array(len);
  } catch(e) { data_u16 = data_code }

  var i;

  for (i = 0; i < len; ++i) {
    c = randCharCode()|0;
    data_code[i] = c;
    data_u8[i] = c;
    data_u16[i] = c;
    data_char[i] = String.fromCharCode(c);
  }
} 
</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
Uint8Array fromCharCode
res = String.fromCharCode.apply(null, data_u8);
pending…
Array fromCharCode
res = String.fromCharCode.apply(null, data_code);
pending…
join
res = data_char.join('');
pending…
append
for (res = '', i = 0; i < len; ++i) {
  res += data_char[i];
}
pending…
append fromCharCode Array
for (res = '', i = 0; i < len; ++i) {
  res += String.fromCharCode(data_code[i]);
}
pending…
Uint16Array fromCharCode
res = String.fromCharCode.apply(null, data_u16);
pending…
append fromCharCode Uint16
for (res = '', i = 0; i < len; ++i) {
  res += String.fromCharCode(data_u16[i]);
}
pending…
append fromCharCode Uint8
for (res = '', i = 0; i < len; ++i) {
  res += String.fromCharCode(data_u8[i]);
}
pending…

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

0 Comments