The primary reason Math.floor
is slower (where it actually is--in some tests I've done it's faster) is that it involves a function call. Older JavaScript implementations couldn't inline function calls. Newer engines can inline the call, or at least make the property lookup faster, but they still need a guard condition in case you (or some other script) overwrote the Math.floor
function. The overhead is minimal though, so there's not much difference in speed.
More importantly though, as was mentioned in several comments, the other methods are not equivalent. They all work by doing bitwise operations. The bitwise operators automatically convert their operands to 32-bit integers by truncating the number. That's fine if the number fits in 32 bits, but JavaScript numbers are 64-bit floats, which could be much larger than 2147483647.
They also give a different result for negative numbers, since converting to integers truncates and Math.floor
always rounds down. For example, Math.floor(-2.1) === -3
, but (-2.1) | (-2.1) === -2
.
If you know you are only dealing with positive numbers less than 2147483648, and you need to squeeze every bit of performance out of your code in older browsers (Make sure it's actually the bottleneck first. It probably isn't.), I would use an even simpler method: x|0
. It doesn't evaluate the variable twice, and it works even if x
is an expression (just be sure to put it in parentheses so you don't run into precedence issues).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…