Javascript Math.ceil(Math.abs()) optimization

前端 未结 6 985
清酒与你
清酒与你 2020-12-10 17:17

I\'m using Math.ceil( Math.abs( x ) ) inside a loop.

Can anyone realize any optimization for this operation? (Bitwise or what?)

You are welcom

6条回答
  •  借酒劲吻你
    2020-12-10 17:56

    Javascript isn't a compiled language like C, so bitwise operations that can work wonders in such languages, aren't so great in JS because numbers are stored as 64 bit floating points. Take a look at this SO post.

    Even then, what you write in JS will get transformed to native code somehow by underlying browser and it might be faster or slower, depending on implementation.

    Since Math.ceil and Math.abs are built in; I'd guess they're heavily optimized, so I doubt you'll be able to get better performance by doing some trickery of your own.

    Bottom line: three things stand in your way of doing it faster:

    1. number representation in JS
    2. fact that it's an interpreted language
    3. functions you use are "native", so they should be fast enough on their own

提交回复
热议问题