Is integer

JavaScript performance comparison

Test case created by ckknight

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function modulo(x) {
      return x % 1 === 0;
    }
   
    function floor(x) {
      return Math.floor(x) === x;
    }
   
    var _floor = Math.floor;
    function cachedFloor(x) {
      return _floor(x) === x;
    }
   
    function bitwiseOr(x) {
      // fails for x > 2**31, x <= -2**31
      return x|0 === x;
    }
   
    function bitwiseRightShift(x) {
      return x >> 0 === x;
    }
   
    function assert(x) {
      if (!x) {
        throw Error();
      }
    }
   
    function test(func) {
      assert(func(0));
      assert(!func(0.5));
      assert(!func(-0.5));
      assert(func(1));
      assert(func(-1));
      assert(func(1234));
      assert(func(-1234));
      assert(func(1e10));
      assert(func(-1e10));
    }
};
</script>

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
x % 1 === 0
test(modulo);
 
pending…
Math.floor(x) === x
test(floor);
 
pending…
_floor(x) === x
test(cachedFloor);
 
pending…
x|0 === x (bad for large ints)
test(bitwiseOr)
pending…
x >> 2 === x (bad for large ints)
test(bitwiseRightShift)
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. Here’s a list of current revisions for this page:

0 comments

Add a comment