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 <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

1 file changed

Lines changed: 9 additions & 5 deletions

File tree

‎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(typeofval1==='number'){
186-
returnNumberIsNaN(val1)&&NumberIsNaN(val2);
185+
// Check for NaN
186+
// eslint-disable-next-line no-self-compare
187+
returnval1!==val1&&val2!==val2;
187188
}
188189
if(typeofval2!=='object'||
189190
typeofval1!=='object'||
@@ -195,8 +196,9 @@ function innerDeepEqual(val1, val2, mode, memos) {
195196
}else{
196197
if(val1===null||typeofval1!=='object'){
197198
return(val2===null||typeofval2!=='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||typeofval2!=='object'){
202204
returnfalse;
@@ -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
returnfalse;
499503
}
500504
}

0 commit comments

Comments
 (0)