toInteger

Benchmark created by Kris Kowal on


Preparation HTML

<script>
  var toInteger1 = function (n) {
      n = +n;
      if (n !== n) // isNaN
          n = -1;
      else if (n !== 0 && n !== (1/0) && n !== -(1/0))
          n = (n > 0 || -1) * Math.floor(Math.abs(n));
      return n;
  };
  
  function toInteger2(value) {
    value = +value;
    return value === 0 || !isFinite(value) ? value || 0 : value - (value % 1);
  }
  
  function test(f) {
      f(1); f(2); f(3); f(4); f(5);
      f(-1); f(-2);
      f(10.0);
      f("10");
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
2
test(toInteger2)
ready
1
test(toInteger1)
ready

Revisions

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

  • Revision 1: published by Kris Kowal on