I'm using Math.ceil( Math.abs( x ) )
inside a loop.
Can anyone realize any optimization for this operation? (Bitwise or what?)
You are welcome to benchmark at jsperf.com
Math.abs doesn't get simpler according to webkit JavaScriptCore
case MathObjectImp::Abs:
result = ( arg < 0 || arg == -0) ? (-arg) : arg;
However ceil uses C's ceil function
case MathObjectImp::Ceil:
result = ::ceil(arg);
so testing on JSpref
http://jsperf.com/math-ceil-vs-bitwise
bitwise is faster
testing @orangedog's answer http://jsperf.com/math-ceil-vs-bitwise/2 Math.ceil is faster
So I guess your best choice is:
var n = Math.abs(x);
var f = (n << 0),
f = f == n ? f : f + 1;
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.
parseInt(Math.abs(x)) + 1
is faster by about 30% on Firefox according to jsperf
As the argument is always positive, the branches in Math.ceil() are unnecessary.
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:
Here's another one, which doesn't need to do any lookup:
((x >= 0 ? x : -x) + 0.5) >> 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With