Math.floor vs shift 2
JavaScript performance comparison
Info
Compares Math.floor(x) to (x >> 0) as a way of converting to an int. They aren't completely equivalent; note that Math.floor(NaN) is NaN, while (NaN >> 0) is 0. Also added a if NaN function.
Preparation code
<script>
Benchmark.prototype.setup = function() {
function a(num) {return Math.floor(num);}
function b(num) {return +num>>0;}
function c(num) {num=+num;return num===num?num>>0:NaN;}
function d(num) {if(+num===NaN)return NaN;return num>>0;}
function e(num) {return Number(Number(num)>>0)}
function f(num) {return Number(num)===num?num===""?0:num>>0:NaN;}
};
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
Test  Ops/sec  

Math.floor 

pending… 
Shift 

pending… 
Shift with Ternary 

pending… 
Shift with If 

pending… 
Shift with Number Conversion 

pending… 
Shift with number coercion 2 

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 Ted Hopp
 Revision 2: published
0 comments