Skip to content

Commit 696b5f8

Browse files
BridgeARaduh95
authored andcommitted
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()`. PR-URL: #57619 Reviewed-By: Jordan Harband <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Chemi Atlow <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Edy Silva <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Rafael Gonzaga <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent 775ee4d commit 696b5f8

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,
@@ -183,7 +182,9 @@ function innerDeepEqual(val1, val2, mode, memos) {
183182
// Check more closely if val1 and val2 are equal.
184183
if (mode !== kLoose) {
185184
if (typeof val1 === 'number') {
186-
return NumberIsNaN(val1) && NumberIsNaN(val2);
185+
// Check for NaN
186+
// eslint-disable-next-line no-self-compare
187+
return val1 !== val1 && val2 !== val2;
187188
}
188189
if (typeof val2 !== 'object' ||
189190
typeof val1 !== 'object' ||
@@ -195,8 +196,9 @@ function innerDeepEqual(val1, val2, mode, memos) {
195196
} else {
196197
if (val1 === null || typeof val1 !== 'object') {
197198
return (val2 === null || typeof val2 !== 'object') &&
198-
// eslint-disable-next-line eqeqeq
199-
(val1 == val2 || (NumberIsNaN(val1) && NumberIsNaN(val2)));
199+
// Check for NaN
200+
// eslint-disable-next-line eqeqeq, no-self-compare
201+
(val1 == val2 || (val1 !== val1 && val2 !== val2));
200202
}
201203
if (val2 === null || typeof val2 !== 'object') {
202204
return false;
@@ -494,7 +496,9 @@ function findLooseMatchingPrimitives(prim) {
494496
// a regular number and not NaN.
495497
// Fall through
496498
case 'number':
497-
if (NumberIsNaN(prim)) {
499+
// Check for NaN
500+
// eslint-disable-next-line no-self-compare
501+
if (prim !== prim) {
498502
return false;
499503
}
500504
}

0 commit comments

Comments
 (0)