substr speed

Benchmark created on


Preparation HTML

<script>
  var i = 'sdgfd23'
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
native substr
var x = parseInt(i.substr(-2, 2));
ready
with charCodeAt
var l = i.length;
var x = (i.charCodeAt(l - 2) - 48) * 10 + i.charCodeAt(l - 1) - 48;
ready
seperate chars
var l = i.length;
var x = parseInt(i[l - 1] + i[l]);
ready
with charCodeAt (save)
var l = i.length;
var z = i.charCodeAt(l - 2);
var x = (z > 48 && z < 59) ? (z - 48) * 10 + i.charCodeAt(l - 1) - 48 : 65;
delete z;
delete l;
ready

Revisions

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