Page 1 of 1

Efficient Math.sign(value) polyfill

Posted: Sat Sep 12, 2020 9:23 am
by CVH
Andrew, all,
stepping trough a lot of code these days ... :roll:

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. :oops:

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. :wink:

Regards,
CVH

Re: Efficient Math.sign(value) polyfill

Posted: Mon Sep 14, 2020 10:57 am
by andrew
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

Re: Efficient Math.sign(value) polyfill

Posted: Mon Sep 14, 2020 4:41 pm
by CVH
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
(43.57 KiB) Downloaded 333 times
Bottom line:
I'll keep stepping in 3-4 steps past it knowing that I will never know it and never can test it. :roll:

Regards,
CVH

Re: Efficient Math.sign(value) polyfill

Posted: Mon Sep 14, 2020 7:48 pm
by andrew
So that's 50ms for 1'000'000 calls. I wouldn't waste time on that.