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

前端 未结 6 987
清酒与你
清酒与你 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 18:06

    x < 0 ? Math.ceil(-x) : Math.ceil(x) produces a 40% speedup in Firefox 3.6 (little difference in the others) while remaining relatively readable.

    Here is the jsPerf page. Ignore the "some bitwise operators" label; the expression above doesn't use any.

提交回复
热议问题