JavaScript fast-floor
Use the double NOT bitwise operator to perform a fast-floor.
Math.floor(123.456) // => 123
~~123.456 // => 123
Pro tip: Be careful with negative numbers!
Math.floor(-123.456) // => 124
~~-123.456 // => 123Written by Florent Cailhol
Related protips
1 Response
Interesting. It does seem faster, to a degree:
http://jsperf.com/math-floor-no-loop & http://jsperf.com/math-floor
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Math
Authors
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#