## Efficient Math.sign(value) polyfill

Please use this forum to post feedback and suggestions related to QCAD.

Moderator: andrew

CVH
Premier Member
Posts: 904
Joined: Wed Sep 27, 2017 4:17 pm
Location: Belgium; Antwerp

### Efficient Math.sign(value) polyfill

Andrew, all,
stepping trough a lot of code these days ...

I pass this polyfill hundred and more times a day.

Code: Select all

``````// In input.js:
sign = Math.sign = function(x) { x = +x; if (x === 0 || isNaN(x)) { return Number(x); } return x > 0 ? 1 : -1; };
``````
It is a three / four step process that returns (NaN / -1 / -0 / 0 / 1) as the sign of a value.

Better seen when you write it as this:

Code: Select all

``````sign = Math.sign = function(x) {
x = +x;
if (x === 0 || isNaN(x)) { return Number(x); }
return x > 0 ? 1 : -1;
};
``````
The 'if' line seems not to end in a ';' but a '}' is fine too.
Out of the blue I would write that as:

Code: Select all

``````sign = Math.sign = function(x) { x = +x; if (x === 0 || isNaN(x)) return Number(x); return x > 0 ? 1 : -1; };
// OR
sign = Math.sign = function(x) {
x = +x;
if (x === 0 || isNaN(x)) return Number(x);
return x > 0 ? 1 : -1;
};
``````
But I can be O so wrong.

I came across this single step solution:

Code: Select all

``````// Efficient Polyfill Math.sign:
// Copy from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/sign
//    If x is NaN, the result is NaN.
//    If x is -0, the result is -0.
//    If x is +0, the result is +0.
//    If x is negative and not -0, the result is -1.
//    If x is positive and not +0, the result is +1.
//

Math.sign = function(x) { return ((x > 0) - (x < 0)) || +x };

//    A more aesthetic pseudo-representation:
//     ( (x > 0) ? 1 : 0 )  // if x is positive, then positive one
//              +           // else (because you can't be both - and +)
//     ( (x < 0) ? -1 : 0 ) // if x is negative, then negative one
//             ||           // if x is 0, -0, or NaN, or not a number,
//             +x           // then the result will be x, (or) if x is
//                          // not a number, then x converts to number
//     No extra type-coercing is needed to make (x > 0) or (x < 0) numbers because
//     subtracting them from each other forces a type conversion from booleans to numbers.
``````
I can't even start to imagine how many signs QCAD evaluates ...

For me it seems free to use and one can find it on many public places.
With or without the reference I include.

Regards,
CVH
QCADpro 3.25.2.1 / win7pro 32 bit / 4GB / 1e generation i7 2.6Ghz / HDD GeForce GT 520

andrew
Posts: 7095
Joined: Fri Mar 30, 2007 6:07 am

### Re: Efficient Math.sign(value) polyfill

Did you benchmark these against each other?

Here's some simple benchmark code for 1'000'000 calls each:

Code: Select all

``````var i;

Math.sign = function(x) { x = +x; if (x === 0 || isNaN(x)) { return Number(x); } return x > 0 ? 1 : -1; };
RDebug.startTimer();
for (i=-500000; i<500000; i++) {
Math.sign(i);
}
RDebug.stopTimer("original polyfill");

Math.sign = function(x) { return ((x > 0) - (x < 0)) || +x };
RDebug.startTimer();
for (i=-500000; i<500000; i++) {
Math.sign(i);
}
RDebug.stopTimer("new polyfill");``````
You can save this as "benchmark.js" and run it with qcad -autostart benchmark.js

CVH
Premier Member
Posts: 904
Joined: Wed Sep 27, 2017 4:17 pm
Location: Belgium; Antwerp

### Re: Efficient Math.sign(value) polyfill

Yes I did ...
Running it against the compiled polyfill of input.js
Also given a baseline with a dummy to time the looping itself.

Long story short.
It is the order of thing and because it runs from within an addon script.
The newer polyfill is definitively faster in all cases if you run it in second place against the compiled.
In every next consecutive run it tends to be slower for real numbers but still faster for ±zeros, NaN and Undefined.
In general slower for strings.

And the Benchmarks differ again depending where the polyfill is implemented.
ECMAScript behaves strange ...
Overview included.
Sign polyfill benchmarks.pdf
Bottom line:
I'll keep stepping in 3-4 steps past it knowing that I will never know it and never can test it.

Regards,
CVH
QCADpro 3.25.2.1 / win7pro 32 bit / 4GB / 1e generation i7 2.6Ghz / HDD GeForce GT 520

andrew