Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 696b5f8

Browse filesBrowse 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 <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 775ee4d commit 696b5f8
Copy full SHA for 696b5f8

File tree

Expand file treeCollapse file tree

1 file changed

+9
-5
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+9
-5
lines changed

‎lib/internal/util/comparisons.js

Copy file name to clipboardExpand all lines: lib/internal/util/comparisons.js
+9-5Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.