toInteger

JavaScript performance comparison

Test case created by Kris Kowal

Preparation code

<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

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
2
test(toInteger2)
pending…
1
test(toInteger1)
pending…

Compare results of other browsers

Revisions

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

0 Comments