Performance of various ways to convert strings to numbers - hex

Benchmark created by T.J. Crowder on


Description

Compares the performance of various ways of converting hex strings to numbers:

  • The unary operator (+str)
  • The Number function
  • parseInt with a radix

Preparation HTML

<div id="theDiv" data-val="0x20"></div>
<script>
  var str = document.getElementById("theDiv").getAttribute("data-val");
  var n;
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Unary operator +
n = +str;
ready
Number function
n = Number(str);
ready
parseInt with radix
n = parseInt(str, 16);
ready

Revisions

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

  • Revision 1: published by T.J. Crowder on