Today's Question:  What weekend projects have you created?        GIVE A SHOUT

Technical Article => Web =>  JavaScript

Why you should be careful about optimizations

  Maël Nison      2012-05-02 11:29:20      2,360    0

In one of my current javascript projects, I have to deal with 3D voxel coordinates. And, sometimes, I have floating points coordinates which must be converted to integers in order to convert them into proper array index.

Javascript is a wonderful language, really. However, implementations are sometimes weird, and the way to do something can sometimes have very huge impacts on the performances. Worse, sometimes the classical way is more expensive than the tricky one.

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.

Source :


Share on Facebook  Share on Twitter  Share on Google+  Share on Weibo  Share on Reddit  Share on Digg  Share on Tumblr 



No comment for this article.


What is procedure programming?

By sonic0002
After viewing this picture, you should understand what procedure programming is. Want to install air conditioner at higher levels? Give them more ladders.