You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The fast path did not anticipate different ways to express a loose
equal string value (e.g., 1n == '+0001'). This is now fixed with the
downside that all primitives that could theoretically have equal
entries must go through a full comparison.
Only some strings, symbols, undefined, null and NaN can be detected
in a fast path as those entries have a strictly limited set of
possible equal entries.
PR-URL: #22495
Reviewed-By: Benjamin Gruenbaum <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: John-David Dalton <[email protected]>
@@ -354,23 +354,52 @@ function setHasEqualElement(set, val1, strict, memo) {
354
354
returnfalse;
355
355
}
356
356
357
-
// Note: we currently run this multiple times for each loose key!
358
-
// This is done to prevent slowing down the average case.
359
-
functionsetHasLoosePrim(a,b,val){
360
-
constaltValues=findLooseMatchingPrimitives(val);
361
-
if(altValues===undefined)
362
-
returnfalse;
357
+
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using
358
+
// Sadly it is not possible to detect corresponding values properly in case the
359
+
// type is a string, number, bigint or boolean. The reason is that those values
360
+
// can match lots of different string values (e.g., 1n == '+00001').
361
+
functionfindLooseMatchingPrimitives(prim){
362
+
switch(typeofprim){
363
+
case'undefined':
364
+
returnnull;
365
+
case'object': // Only pass in null as object!
366
+
returnundefined;
367
+
case'symbol':
368
+
returnfalse;
369
+
case'string':
370
+
prim=+prim;
371
+
// Loose equal entries exist only if the string is possible to convert to
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using
415
-
functionfindLooseMatchingPrimitives(prim){
416
-
switch(typeofprim){
417
-
case'number':
418
-
if(prim===0){
419
-
return['','0',false];
420
-
}
421
-
if(prim===1){
422
-
return['1',true];
423
-
}
424
-
return[''+prim];
425
-
case'string':
426
-
if(prim===''||prim==='0'){
427
-
return[0,false];
428
-
}
429
-
if(prim==='1'){
430
-
return[1,true];
431
-
}
432
-
constnumber=+prim;
433
-
if(''+number===prim){
434
-
return[number];
435
-
}
436
-
return;
437
-
case'undefined':
438
-
return[null];
439
-
case'object': // Only pass in null as object!
440
-
return[undefined];
441
-
case'boolean':
442
-
if(prim===false){
443
-
return['','0',0];
444
-
}
445
-
return['1',1];
446
-
}
447
-
}
448
-
449
-
// This is a ugly but relatively fast way to determine if a loose equal entry
450
-
// currently has a correspondent matching entry. Otherwise checking for such
451
-
// values would be way more expensive (O(n^2)).
452
-
// Note: we currently run this multiple times for each loose key!
453
-
// This is done to prevent slowing down the average case.
0 commit comments