parseFloat alternative (v4)

Revision 4 of this benchmark created by pmilkman on


Description

This approach works if you can guarantee that the number inside the string is properly formed. IE "-111.111" works, but not "-111.111.1" or "34234px". For those, you stick with parseFloat().

Preparation HTML

<script>
  var a = "111.111";
  
  function pFA(a) {
   var b = ~~a;
   return b + (a - b);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
parseFloat
a = parseFloat(a);
ready
~~
a = ~~a + (a - ~~a);
ready
Cache ~~
var b = ~~a;
a = b + (a - b);
ready
Func call speed loss?
a = pFA(a);
ready
- 0
a = a - 0
ready
plus
a = +a
ready

Revisions

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

  • Revision 1: published by pmilkman on
  • Revision 2: published by pmilkman on
  • Revision 3: published by pmilkman on
  • Revision 4: published by pmilkman on