Efficient Math.sign(value) polyfill

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

Moderator: andrew

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

Efficient Math.sign(value) polyfill

Post by CVH » Sat Sep 12, 2020 9:23 am

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
QCADpro 3.24.3.10 / win7pro 32 bit / 4GB / 1e generation i7 2.6Ghz / HDD GeForce GT 520

User avatar
andrew
Site Admin
Posts: 7035
Joined: Fri Mar 30, 2007 6:07 am

Re: Efficient Math.sign(value) polyfill

Post by andrew » Mon Sep 14, 2020 10:57 am

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: 855
Joined: Wed Sep 27, 2017 4:17 pm
Location: Belgium; Antwerp

Re: Efficient Math.sign(value) polyfill

Post by CVH » Mon Sep 14, 2020 4:41 pm

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 11 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
QCADpro 3.24.3.10 / win7pro 32 bit / 4GB / 1e generation i7 2.6Ghz / HDD GeForce GT 520

User avatar
andrew
Site Admin
Posts: 7035
Joined: Fri Mar 30, 2007 6:07 am

Re: Efficient Math.sign(value) polyfill

Post by andrew » Mon Sep 14, 2020 7:48 pm

So that's 50ms for 1'000'000 calls. I wouldn't waste time on that.

Post Reply

Return to “QCAD Suggestions and Feedback”