disfated disfated - 1 month ago 16
Javascript Question

Number.sign() in javascript

Wonder if there are any nontrivial ways of finding number's sign (signum function)?

May be shorter / faster / more elegant solutions than the obvious one

var sign = number > 0 ? 1 : number < 0 ? -1 : 0;





Short excerpt



Use this and you'll be safe and fast

function sign(x) {
return typeof x === 'number' ? x ? x < 0 ? -1 : 1 : x === x ? 0 : NaN : NaN;
}





Results



For now we have these solutions:




1. Obvious and fast

function sign(x) { return x > 0 ? 1 : x < 0 ? -1 : 0; }





1.1. Modification from kbec - one type cast less, more performant, shorter [fastest]

function sign(x) { return x ? x < 0 ? -1 : 1 : 0; }


caution:
sign("0") -> 1





2. Elegant, short, not so fast [slowest]

function sign(x) { return x && x / Math.abs(x); }


caution:
sign(+-Infinity) -> NaN
,
sign("0") -> NaN


As of
Infinity
is a legal number in JS this solution doesn't seem fully correct.




3. The art... but very slow [slowest]

function sign(x) { return (x > 0) - (x < 0); }





4. Using bit-shift

fast, but
sign(-Infinity) -> 0


function sign(x) { return (x >> 31) + (x > 0 ? 1 : 0); }





5. Type-safe [megafast]

! Seems like browsers (especially chrome's v8) make some magic optimizations and this solution turns out to be much more performant than others, even than (1.1) despite it contains 2 extra operations and logically never can't be faster.

function sign(x) {
return typeof x === 'number' ? x ? x < 0 ? -1 : 1 : x === x ? 0 : NaN : NaN;
}





Tools





Improvements are welcome!




[Offtopic] Accepted answer




  • Andrey Tarantsov - +100 for the art, but sadly it is about 5 times slower than the obvious approach

  • Frédéric Hamidi - somehow the most upvoted answer (for the time writing) and it's kinda cool, but it's definitely not how things should be done, imho. Also it doesn't correctly handle Infinity numbers, which are also numbers, you know.

  • kbec - is an improvement of the obvious solution. Not that revolutionary, but taking all together I consider this approach the best. Vote for him :)


Answer

More elegant version of fast solution:

var sign = number?number<0?-1:1:0

Regards