Is integer
JavaScript performance comparison
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.
| Test | Ops/sec | |
|---|---|---|
x % 1 === 0 |
|
pending… |
Math.floor(x) === x |
|
pending… |
_floor(x) === x |
|
pending… |
x|0 === x (bad for large ints) |
|
pending… |
x >> 2 === x (bad for large ints) |
|
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:
- Revision 1: published by ckknight
- Revision 2: published
0 comments