Skip to content

Commit 2e9fb6e

Browse files
BridgeARRafaelGSS
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 <ljharb@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Chemi Atlow <chemi@atlow.co.il> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Edy Silva <edigleyssonsilva@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 5f9cc5e commit 2e9fb6e

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;
@@ -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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy