So, some people have made performance comparisons on jsperf to check
which is the fastest way to do. Most of these tests say that one of the
fastest way to floor a number is to use the bitwise operator (
|). That’s right. But that’s also wrong.
Actually, the bitwise operator doesn’t make a floor operation: it’s only one of the side effects. And it means that we couldn’t rely on it. I’ve used it in my voxel project, and spend many hours to figure the problem. If I was working with positive coordinates, everything was just fine. However, if I tried to use negative coordinates, the rendering was messed up.
After much debugging, I’ve finally understood what’s going on. It was
the bitwise. In fact, rather than flooring the number, the bitwise just
truncate it’s floating part. So
5.4 | 0 will gives
5, which is a flooring operation. However,
-5.4 will give
-5. And that’s a ceiling operation.
So, having prematuraly optimized my code, I’ve spent a lot of time on a very silly problem.
Be careful next time you will want to use a tricky way to get some fps.