Created
August 22, 2011 14:05
-
-
Save Olical/1162452 to your computer and use it in GitHub Desktop.
Very fast rounding in JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Number.round = function() { | |
return (this + 0.5) << 0; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for the trick!
You know what surprised me? That when you put the bitshifting stuff in a function, it is faster than applying it every time on the number literally. Have a look:
gave me this in newest Firefox:
In node.js however it is nearly identical speed for all of them: