Last Updated: February 25, 2016
·
1.616K
· ooflorent

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 // => 123

1 Response
Add your 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
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