String.fromCharCode Test

JavaScript performance comparison

Revision 12 of this test case created by 4esn0k

Preparation code

<script>
  var xxx = [];
  var j = -1;
  while (++j < 1024) {
    xxx.push(Math.floor(Math.random() * 255));
  }
  var s = String.fromCharCode.apply(null, xxx);

var chars = new Array(32);
var textDecoder = self.TextDecoder ? new TextDecoder("utf-16le") : null;
var uint8array = self.Uint8Array ? new Uint8Array(128 * 2) : null;

</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
Using a loop
var i = -1;
var l = s.length;
var r = "";
while (++i < l) {
  r += String.fromCharCode(s.charCodeAt(i));
}
if (r !== s) {
  throw new Error();
}
pending…
TextDecoder
var i = -1;
var l = s.length;
var k = -1;
var bufferLength = uint8array.length;
var r = "";
while (++i < l) {
  uint8array[++k] = s.charCodeAt(i) & 0xff;
  uint8array[++k] = s.charCodeAt(i) >>> 8;
  if (k === bufferLength - 1) {
    r += textDecoder.decode(uint8array);
    k = -1;
  }
}
r += textDecoder.decode(uint8array).slice(0, k + 1);
if (r !== s) {
  throw new Error();
}
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