Skip to content

Commit 657f818

Browse files
BridgeARnodejs-github-bot
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 b9c9bf4 commit 657f818

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

lib/internal/util/comparisons.js

+9-5
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;
@@ -501,7 +503,9 @@ function findLooseMatchingPrimitives(prim) {
501503
// a regular number and not NaN.
502504
// Fall through
503505
case 'number':
504-
if (NumberIsNaN(prim)) {
506+
// Check for NaN
507+
// eslint-disable-next-line no-self-compare
508+
if (prim !== prim) {
505509
return false;
506510
}
507511
}

0 commit comments

Comments
 (0)