parsing-numbers-on-the-fly

Benchmark created on


Preparation HTML

<script>
  var s = '';
  var spaces = '';
  for (var i = 0; i < 36; ++i) spaces += ' ';
  for (var i = 0; i < 1000; i++) s += '36\u0000' + spaces;
  s.charCodeAt(0);
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
on the fly
var i = 0;
var sl = s.length;
var l = 0;
var bias = "0".charCodeAt(0);
while (i < sl) {
 var code = s.charCodeAt(i);
 if (code == 0) {
  i += l + 1;
  l = 0;
 } else {
  l = 10 * l + (code - bias);
  i++;
 }
}

if (i != sl) console.log("failure");
ready
concat and parse with Number
var i = 0;
var sl = s.length;
var l = '';
while (i < sl) {
 var ch = s[i];
 if (ch == "\u0000") {
  i += Number(l) + 1;
  l = '';
 } else {
  l += ch;
  i++;
 }
}

if (i != sl) console.log("failure");
ready
concat and coerce with bit-or
var i = 0;
var sl = s.length;
var l = '';
while (i < sl) {
 var ch = s[i];
 if (ch == "\u0000") {
  i += (l | 0) + 1;
  l = '';
 } else {
  l += ch;
  i++;
 }
}

if (i != sl) console.log("failure");
ready

Revisions

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