Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 634
Dan Avatar asked Jan 06 '11 12:01

Dan


5 Answers

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;
like image 170
Amjad Masad Avatar answered Oct 18 '22 23:10

Amjad Masad


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.

like image 43
PleaseStand Avatar answered Oct 18 '22 22:10

PleaseStand


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.

like image 30
OrangeDog Avatar answered Oct 19 '22 00:10

OrangeDog


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
like image 37
darioo Avatar answered Oct 19 '22 00:10

darioo


Here's another one, which doesn't need to do any lookup:

((x >= 0 ? x : -x) + 0.5) >> 0
like image 45
Christoph Avatar answered Oct 19 '22 00:10

Christoph