Skip to content

Commit f46895a

Browse files
committed
assert,util: improve unequal number comparison performance
This improves the performance to compare unequal numbers while doing a deep equal comparison. Comparing for NaN is faster by checking `variable !== variable` than by using `Number.isNaN()`.
1 parent 686deb7 commit f46895a

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/internal/util/comparisons.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ const {
88
BooleanPrototypeValueOf,
99
DatePrototypeGetTime,
1010
Error,
11-
NumberIsNaN,
1211
NumberPrototypeValueOf,
1312
ObjectGetOwnPropertySymbols: getOwnSymbols,
1413
ObjectGetPrototypeOf,
@@ -185,7 +184,9 @@ function innerDeepEqual(val1, val2, mode, memos) {
185184
// Check more closely if val1 and val2 are equal.
186185
if (mode !== kLoose) {
187186
if (typeof val1 === 'number') {
188-
return NumberIsNaN(val1) && NumberIsNaN(val2);
187+
// Check for NaN
188+
// eslint-disable-next-line no-self-compare
189+
return val1 !== val1 && val2 !== val2;
189190
}
190191
if (typeof val2 !== 'object' ||
191192
typeof val1 !== 'object' ||
@@ -197,8 +198,9 @@ function innerDeepEqual(val1, val2, mode, memos) {
197198
} else {
198199
if (val1 === null || typeof val1 !== 'object') {
199200
return (val2 === null || typeof val2 !== 'object') &&
200-
// eslint-disable-next-line eqeqeq
201-
(val1 == val2 || (NumberIsNaN(val1) && NumberIsNaN(val2)));
201+
// Check for NaN
202+
// eslint-disable-next-line eqeqeq, no-self-compare
203+
(val1 == val2 || (val1 !== val1 && val2 !== val2));
202204
}
203205
if (val2 === null || typeof val2 !== 'object') {
204206
return false;
@@ -498,7 +500,9 @@ function findLooseMatchingPrimitives(prim) {
498500
// a regular number and not NaN.
499501
// Fall through
500502
case 'number':
501-
if (NumberIsNaN(prim)) {
503+
// Check for NaN
504+
// eslint-disable-next-line no-self-compare
505+
if (prim !== prim) {
502506
return false;
503507
}
504508
}

0 commit comments

Comments
 (0)