All files / lib/internal/util comparisons.js

99.52% Statements 623/626
99.35% Branches 305/307
100% Functions 19/19
99.52% Lines 623/626

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 62710x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 56x 56x 56x 10x 8x 8x 1x 1x 8x 1200018x 2x 2x 1200018x 5x 8x 10x 2683x 2683x 4x 4x 2679x 2679x 2683x 10x 16x 16x 16x 16x 10x 80x 80x 20x 18x 20x 20x 80x 28x 28x 28x 80x 15x 15x 15x 80x 5x 5x 5x 12x 12x 12x 12x 1x 1x 80x 10x 2697x 2697x 2697x 2697x 2695x 2695x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 23x 6x 2x 2x 6x 1x 1x       2697x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 97127x 97127x 97127x 69377x 69377x 69377x 69377x 27750x 27750x 28992x 26977x 125x 125x 125x 26977x 20x 20x 26970x 406x 406x 80361x 773x 170x 160x 160x 160x 10x 10x 773x 10x 10x 773x 27019x 27019x 27019x 80350x 201x 201x 26818x 38265x 22050x 22050x 36x 36x 22050x 22050x 22050x 22050x 6x 6x 22007x 80342x 1379x 4765x 42x 42x 9x 9x 3389x 60x 29x 29x 3347x 32x 32x 32x 32x 32x 15x 15x 3287x 2697x 2697x 2697x 8x 3x 3x 2697x 15x 15x 2673x 2673x 2673x 2697x 2697x 2697x 2697x 6x 6x 2667x 3255x 197x 12x 12x 185x 558x 225x 10x 10x 215x 361x 16x 8x 8x 136x 80x 34x 34x 120x 40x 40x 40x 40x 40x 40x 40x 40x 40x 16x 16x 159x 97127x 10x 8x 8x 8x 10x 26612x 26612x 26612x 26612x 26612x 26612x 26612x 26612x 26612x 1938x 1938x 1938x 1938x 1938x 38x 38x 1938x 26574x 26574x 26574x 26609x 7382x 4x 4x 7382x 26570x 26612x 1647x 1647x 35x 35x 48x 48x 32x 8x 8x 24x 24x 48x 1x 1x 48x 26x 35x 35x 1x 1x 1647x 1612x 1612x 1612x 2x 2x 1612x 1647x 26558x 26609x 25263x 25263x 26612x 2992x 2992x 23566x 23566x 23915x 22669x 22669x 22669x 22669x 22669x 26609x 897x 897x 897x 897x 897x 51x 51x 45x 45x 51x 852x 852x 23521x 23521x 23521x 23521x 23521x 23521x 23521x 23521x 23521x 23521x 26612x 10x 177x 177x 177x 211x 141x 141x 141x 141x 211x 36x 36x 177x 10x 10x 10x 10x 10x 78x 78x 78x 4x 78x 3x 78x 2x 78x 26x 78x 78x 78x 78x 54x 4x 4x 78x 65x 78x 10x 29x 29x 29x 29x 2x 29x 29x 10x 49x 49x 49x 44x 44x 5x 49x 49x 1x 1x 49x 49x 10x 161x 161x 161x 161x 161x 494x 494x 494x 494x 163x 100x 100x 163x 163x 163x 163x 163x 494x 55x 55x 29x 29x 44x 4x 4x 25x 44x 10x 10x 25x 25x 494x 131x 161x 108x 289x 289x 289x 153x 153x 289x 136x 136x 7x 7x 289x 72x 72x 23x 23x 161x 10x 121x 121x 121x 121x 121x 147x 147x 94x 94x 94x 147x 27x 27x 121x 10x 190x 190x 190x 190x 388x 93x 60x 60x 93x 388x 295x 295x 295x 295x 295x 115x 115x 49x 49x 49x 85x 85x 27x 27x 46x 46x 295x 388x 121x 190x 81x 230x 79x 79x 230x 81x 151x 151x 14x 14x 230x 54x 54x 40x 40x 190x 10x 23521x 23521x 23521x 23521x 23521x 23521x 161x 68x 68x 23521x 190x 98x 98x 23360x 21905x 42519x 42503x 42503x 32x 32x 42519x 2x 16x 14x 14x 14x 14x 14x 14x 6x 6x 14x 14x 2x 2x 6x 6x 42519x 21905x 23307x 23307x 23307x 23518x 7306x 7306x 63x 63x 7306x 23244x 23521x 10x 515x 515x 515x 10x 46037x 46037x 46037x 10x 10x 10x 10x 10x  
'use strict';
 
const {
  ArrayIsArray,
  BigIntPrototypeValueOf,
  BooleanPrototypeValueOf,
  DatePrototypeGetTime,
  Error,
  Map,
  NumberIsNaN,
  NumberPrototypeValueOf,
  ObjectGetOwnPropertySymbols,
  ObjectGetPrototypeOf,
  ObjectIs,
  ObjectKeys,
  ObjectPrototypeHasOwnProperty,
  ObjectPrototypePropertyIsEnumerable,
  ObjectPrototypeToString,
  Set,
  StringPrototypeValueOf,
  SymbolPrototypeValueOf,
  SymbolToStringTag,
} = primordials;
 
const { compare } = internalBinding('buffer');
const assert = require('internal/assert');
const types = require('internal/util/types');
const {
  isAnyArrayBuffer,
  isArrayBufferView,
  isDate,
  isMap,
  isRegExp,
  isSet,
  isNativeError,
  isBoxedPrimitive,
  isNumberObject,
  isStringObject,
  isBooleanObject,
  isBigIntObject,
  isSymbolObject,
  isFloat32Array,
  isFloat64Array,
  isUint8Array,
  isUint8ClampedArray,
  isUint16Array,
  isUint32Array,
  isInt8Array,
  isInt16Array,
  isInt32Array,
  isBigInt64Array,
  isBigUint64Array
} = types;
const {
  getOwnNonIndexProperties,
  propertyFilter: {
    ONLY_ENUMERABLE,
    SKIP_SYMBOLS
  }
} = internalBinding('util');
 
const kStrict = true;
const kLoose = false;
 
const kNoIterator = 0;
const kIsArray = 1;
const kIsSet = 2;
const kIsMap = 3;
 
// Check if they have the same source and flags
function areSimilarRegExps(a, b) {
  return a.source === b.source && a.flags === b.flags;
}
 
function areSimilarFloatArrays(a, b) {
  if (a.byteLength !== b.byteLength) {
    return false;
  }
  for (let offset = 0; offset < a.byteLength; offset++) {
    if (a[offset] !== b[offset]) {
      return false;
    }
  }
  return true;
}
 
function areSimilarTypedArrays(a, b) {
  if (a.byteLength !== b.byteLength) {
    return false;
  }
  return compare(new Uint8Array(a.buffer, a.byteOffset, a.byteLength),
                 new Uint8Array(b.buffer, b.byteOffset, b.byteLength)) === 0;
}
 
function areEqualArrayBuffers(buf1, buf2) {
  return buf1.byteLength === buf2.byteLength &&
    compare(new Uint8Array(buf1), new Uint8Array(buf2)) === 0;
}
 
function isEqualBoxedPrimitive(val1, val2) {
  if (isNumberObject(val1)) {
    return isNumberObject(val2) &&
           ObjectIs(NumberPrototypeValueOf(val1),
                    NumberPrototypeValueOf(val2));
  }
  if (isStringObject(val1)) {
    return isStringObject(val2) &&
           StringPrototypeValueOf(val1) === StringPrototypeValueOf(val2);
  }
  if (isBooleanObject(val1)) {
    return isBooleanObject(val2) &&
           BooleanPrototypeValueOf(val1) === BooleanPrototypeValueOf(val2);
  }
  if (isBigIntObject(val1)) {
    return isBigIntObject(val2) &&
           BigIntPrototypeValueOf(val1) === BigIntPrototypeValueOf(val2);
  }
  if (isSymbolObject(val1)) {
    return isSymbolObject(val2) &&
          SymbolPrototypeValueOf(val1) === SymbolPrototypeValueOf(val2);
  }
  /* c8 ignore next */
  assert.fail(`Unknown boxed type ${val1}`);
}
 
function isIdenticalTypedArrayType(a, b) {
  // Fast path to reduce type checks in the common case.
  const check = types[`is${a[SymbolToStringTag]}`];
  if (check !== undefined && check(a)) {
    return check(b);
  }
  // Manipulated Symbol.toStringTag.
  for (const check of [
    isUint16Array,
    isUint32Array,
    isInt8Array,
    isInt16Array,
    isInt32Array,
    isFloat32Array,
    isFloat64Array,
    isBigInt64Array,
    isBigUint64Array,
    isUint8ClampedArray,
    isUint8Array
  ]) {
    if (check(a)) {
      return check(b);
    }
  }
  /* c8 ignore next */
  assert.fail(
    `Unknown TypedArray type checking ${a[SymbolToStringTag]} ${a}\n` +
    `and ${b[SymbolToStringTag]} ${b}`
  );
}
 
// Notes: Type tags are historical [[Class]] properties that can be set by
// FunctionTemplate::SetClassName() in C++ or Symbol.toStringTag in JS
// and retrieved using Object.prototype.toString.call(obj) in JS
// See https://tc39.github.io/ecma262/#sec-object.prototype.tostring
// for a list of tags pre-defined in the spec.
// There are some unspecified tags in the wild too (e.g. typed array tags).
// Since tags can be altered, they only serve fast failures
//
// For strict comparison, objects should have
// a) The same built-in type tag.
// b) The same prototypes.
 
function innerDeepEqual(val1, val2, strict, memos) {
  // All identical values are equivalent, as determined by ===.
  if (val1 === val2) {
    if (val1 !== 0)
      return true;
    return strict ? ObjectIs(val1, val2) : true;
  }
 
  // Check more closely if val1 and val2 are equal.
  if (strict) {
    if (typeof val1 !== 'object') {
      return typeof val1 === 'number' && NumberIsNaN(val1) &&
        NumberIsNaN(val2);
    }
    if (typeof val2 !== 'object' || val1 === null || val2 === null) {
      return false;
    }
    if (ObjectGetPrototypeOf(val1) !== ObjectGetPrototypeOf(val2)) {
      return false;
    }
  } else {
    if (val1 === null || typeof val1 !== 'object') {
      if (val2 === null || typeof val2 !== 'object') {
        // eslint-disable-next-line eqeqeq
        return val1 == val2 || (NumberIsNaN(val1) && NumberIsNaN(val2));
      }
      return false;
    }
    if (val2 === null || typeof val2 !== 'object') {
      return false;
    }
  }
  const val1Tag = ObjectPrototypeToString(val1);
  const val2Tag = ObjectPrototypeToString(val2);
 
  if (val1Tag !== val2Tag) {
    return false;
  }
 
  if (ArrayIsArray(val1)) {
    // Check for sparse arrays and general fast path
    if (!ArrayIsArray(val2) || val1.length !== val2.length) {
      return false;
    }
    const filter = strict ? ONLY_ENUMERABLE : ONLY_ENUMERABLE | SKIP_SYMBOLS;
    const keys1 = getOwnNonIndexProperties(val1, filter);
    const keys2 = getOwnNonIndexProperties(val2, filter);
    if (keys1.length !== keys2.length) {
      return false;
    }
    return keyCheck(val1, val2, strict, memos, kIsArray, keys1);
  } else if (val1Tag === '[object Object]') {
    return keyCheck(val1, val2, strict, memos, kNoIterator);
  } else if (isDate(val1)) {
    if (!isDate(val2) ||
        DatePrototypeGetTime(val1) !== DatePrototypeGetTime(val2)) {
      return false;
    }
  } else if (isRegExp(val1)) {
    if (!isRegExp(val2) || !areSimilarRegExps(val1, val2)) {
      return false;
    }
  } else if (isNativeError(val1) || val1 instanceof Error) {
    // Do not compare the stack as it might differ even though the error itself
    // is otherwise identical.
    if ((!isNativeError(val2) && !(val2 instanceof Error)) ||
        val1.message !== val2.message ||
        val1.name !== val2.name) {
      return false;
    }
  } else if (isArrayBufferView(val1)) {
    if (!isIdenticalTypedArrayType(val1, val2))
      return false;
    if (!strict && (isFloat32Array(val1) || isFloat64Array(val1))) {
      if (!areSimilarFloatArrays(val1, val2)) {
        return false;
      }
    } else if (!areSimilarTypedArrays(val1, val2)) {
      return false;
    }
    // Buffer.compare returns true, so val1.length === val2.length. If they both
    // only contain numeric keys, we don't need to exam further than checking
    // the symbols.
    const filter = strict ? ONLY_ENUMERABLE : ONLY_ENUMERABLE | SKIP_SYMBOLS;
    const keys1 = getOwnNonIndexProperties(val1, filter);
    const keys2 = getOwnNonIndexProperties(val2, filter);
    if (keys1.length !== keys2.length) {
      return false;
    }
    return keyCheck(val1, val2, strict, memos, kNoIterator, keys1);
  } else if (isSet(val1)) {
    if (!isSet(val2) || val1.size !== val2.size) {
      return false;
    }
    return keyCheck(val1, val2, strict, memos, kIsSet);
  } else if (isMap(val1)) {
    if (!isMap(val2) || val1.size !== val2.size) {
      return false;
    }
    return keyCheck(val1, val2, strict, memos, kIsMap);
  } else if (isAnyArrayBuffer(val1)) {
    if (!isAnyArrayBuffer(val2) || !areEqualArrayBuffers(val1, val2)) {
      return false;
    }
  } else if (isBoxedPrimitive(val1)) {
    if (!isEqualBoxedPrimitive(val1, val2)) {
      return false;
    }
  } else if (ArrayIsArray(val2) ||
             isArrayBufferView(val2) ||
             isSet(val2) ||
             isMap(val2) ||
             isDate(val2) ||
             isRegExp(val2) ||
             isAnyArrayBuffer(val2) ||
             isBoxedPrimitive(val2) ||
             isNativeError(val2) ||
             val2 instanceof Error) {
    return false;
  }
  return keyCheck(val1, val2, strict, memos, kNoIterator);
}
 
function getEnumerables(val, keys) {
  return keys.filter((k) => ObjectPrototypePropertyIsEnumerable(val, k));
}
 
function keyCheck(val1, val2, strict, memos, iterationType, aKeys) {
  // For all remaining Object pairs, including Array, objects and Maps,
  // equivalence is determined by having:
  // a) The same number of owned enumerable properties
  // b) The same set of keys/indexes (although not necessarily the same order)
  // c) Equivalent values for every corresponding key/index
  // d) For Sets and Maps, equal contents
  // Note: this accounts for both named and indexed properties on Arrays.
  if (arguments.length === 5) {
    aKeys = ObjectKeys(val1);
    const bKeys = ObjectKeys(val2);
 
    // The pair must have the same number of owned properties.
    if (aKeys.length !== bKeys.length) {
      return false;
    }
  }
 
  // Cheap key test
  let i = 0;
  for (; i < aKeys.length; i++) {
    if (!ObjectPrototypeHasOwnProperty(val2, aKeys[i])) {
      return false;
    }
  }
 
  if (strict && arguments.length === 5) {
    const symbolKeysA = ObjectGetOwnPropertySymbols(val1);
    if (symbolKeysA.length !== 0) {
      let count = 0;
      for (i = 0; i < symbolKeysA.length; i++) {
        const key = symbolKeysA[i];
        if (ObjectPrototypePropertyIsEnumerable(val1, key)) {
          if (!ObjectPrototypePropertyIsEnumerable(val2, key)) {
            return false;
          }
          aKeys.push(key);
          count++;
        } else if (ObjectPrototypePropertyIsEnumerable(val2, key)) {
          return false;
        }
      }
      const symbolKeysB = ObjectGetOwnPropertySymbols(val2);
      if (symbolKeysA.length !== symbolKeysB.length &&
          getEnumerables(val2, symbolKeysB).length !== count) {
        return false;
      }
    } else {
      const symbolKeysB = ObjectGetOwnPropertySymbols(val2);
      if (symbolKeysB.length !== 0 &&
          getEnumerables(val2, symbolKeysB).length !== 0) {
        return false;
      }
    }
  }
 
  if (aKeys.length === 0 &&
      (iterationType === kNoIterator ||
        (iterationType === kIsArray && val1.length === 0) ||
        val1.size === 0)) {
    return true;
  }
 
  // Use memos to handle cycles.
  if (memos === undefined) {
    memos = {
      val1: new Map(),
      val2: new Map(),
      position: 0
    };
  } else {
    // We prevent up to two map.has(x) calls by directly retrieving the value
    // and checking for undefined. The map can only contain numbers, so it is
    // safe to check for undefined only.
    const val2MemoA = memos.val1.get(val1);
    if (val2MemoA !== undefined) {
      const val2MemoB = memos.val2.get(val2);
      if (val2MemoB !== undefined) {
        return val2MemoA === val2MemoB;
      }
    }
    memos.position++;
  }
 
  memos.val1.set(val1, memos.position);
  memos.val2.set(val2, memos.position);
 
  const areEq = objEquiv(val1, val2, strict, aKeys, memos, iterationType);
 
  memos.val1.delete(val1);
  memos.val2.delete(val2);
 
  return areEq;
}
 
function setHasEqualElement(set, val1, strict, memo) {
  // Go looking.
  for (const val2 of set) {
    if (innerDeepEqual(val1, val2, strict, memo)) {
      // Remove the matching element to make sure we do not check that again.
      set.delete(val2);
      return true;
    }
  }
 
  return false;
}
 
// See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Equality_comparisons_and_sameness#Loose_equality_using
// Sadly it is not possible to detect corresponding values properly in case the
// type is a string, number, bigint or boolean. The reason is that those values
// can match lots of different string values (e.g., 1n == '+00001').
function findLooseMatchingPrimitives(prim) {
  switch (typeof prim) {
    case 'undefined':
      return null;
    case 'object': // Only pass in null as object!
      return undefined;
    case 'symbol':
      return false;
    case 'string':
      prim = +prim;
      // Loose equal entries exist only if the string is possible to convert to
      // a regular number and not NaN.
      // Fall through
    case 'number':
      if (NumberIsNaN(prim)) {
        return false;
      }
  }
  return true;
}
 
function setMightHaveLoosePrim(a, b, prim) {
  const altValue = findLooseMatchingPrimitives(prim);
  if (altValue != null)
    return altValue;
 
  return b.has(altValue) && !a.has(altValue);
}
 
function mapMightHaveLoosePrim(a, b, prim, item, memo) {
  const altValue = findLooseMatchingPrimitives(prim);
  if (altValue != null) {
    return altValue;
  }
  const curB = b.get(altValue);
  if ((curB === undefined && !b.has(altValue)) ||
      !innerDeepEqual(item, curB, false, memo)) {
    return false;
  }
  return !a.has(altValue) && innerDeepEqual(item, curB, false, memo);
}
 
function setEquiv(a, b, strict, memo) {
  // This is a lazily initiated Set of entries which have to be compared
  // pairwise.
  let set = null;
  for (const val of a) {
    // Note: Checking for the objects first improves the performance for object
    // heavy sets but it is a minor slow down for primitives. As they are fast
    // to check this improves the worst case scenario instead.
    if (typeof val === 'object' && val !== null) {
      if (set === null) {
        set = new Set();
      }
      // If the specified value doesn't exist in the second set its an not null
      // object (or non strict only: a not matching primitive) we'll need to go
      // hunting for something thats deep-(strict-)equal to it. To make this
      // O(n log n) complexity we have to copy these values in a new set first.
      set.add(val);
    } else if (!b.has(val)) {
      if (strict)
        return false;
 
      // Fast path to detect missing string, symbol, undefined and null values.
      if (!setMightHaveLoosePrim(a, b, val)) {
        return false;
      }
 
      if (set === null) {
        set = new Set();
      }
      set.add(val);
    }
  }
 
  if (set !== null) {
    for (const val of b) {
      // We have to check if a primitive value is already
      // matching and only if it's not, go hunting for it.
      if (typeof val === 'object' && val !== null) {
        if (!setHasEqualElement(set, val, strict, memo))
          return false;
      } else if (!strict &&
                 !a.has(val) &&
                 !setHasEqualElement(set, val, strict, memo)) {
        return false;
      }
    }
    return set.size === 0;
  }
 
  return true;
}
 
function mapHasEqualEntry(set, map, key1, item1, strict, memo) {
  // To be able to handle cases like:
  //   Map([[{}, 'a'], [{}, 'b']]) vs Map([[{}, 'b'], [{}, 'a']])
  // ... we need to consider *all* matching keys, not just the first we find.
  for (const key2 of set) {
    if (innerDeepEqual(key1, key2, strict, memo) &&
      innerDeepEqual(item1, map.get(key2), strict, memo)) {
      set.delete(key2);
      return true;
    }
  }
 
  return false;
}
 
function mapEquiv(a, b, strict, memo) {
  let set = null;
 
  for (const [key, item1] of a) {
    if (typeof key === 'object' && key !== null) {
      if (set === null) {
        set = new Set();
      }
      set.add(key);
    } else {
      // By directly retrieving the value we prevent another b.has(key) check in
      // almost all possible cases.
      const item2 = b.get(key);
      if (((item2 === undefined && !b.has(key)) ||
          !innerDeepEqual(item1, item2, strict, memo))) {
        if (strict)
          return false;
        // Fast path to detect missing string, symbol, undefined and null
        // keys.
        if (!mapMightHaveLoosePrim(a, b, key, item1, memo))
          return false;
        if (set === null) {
          set = new Set();
        }
        set.add(key);
      }
    }
  }
 
  if (set !== null) {
    for (const [key, item] of b) {
      if (typeof key === 'object' && key !== null) {
        if (!mapHasEqualEntry(set, a, key, item, strict, memo))
          return false;
      } else if (!strict &&
                 (!a.has(key) ||
                   !innerDeepEqual(a.get(key), item, false, memo)) &&
                 !mapHasEqualEntry(set, a, key, item, false, memo)) {
        return false;
      }
    }
    return set.size === 0;
  }
 
  return true;
}
 
function objEquiv(a, b, strict, keys, memos, iterationType) {
  // Sets and maps don't have their entries accessible via normal object
  // properties.
  let i = 0;
 
  if (iterationType === kIsSet) {
    if (!setEquiv(a, b, strict, memos)) {
      return false;
    }
  } else if (iterationType === kIsMap) {
    if (!mapEquiv(a, b, strict, memos)) {
      return false;
    }
  } else if (iterationType === kIsArray) {
    for (; i < a.length; i++) {
      if (ObjectPrototypeHasOwnProperty(a, i)) {
        if (!ObjectPrototypeHasOwnProperty(b, i) ||
            !innerDeepEqual(a[i], b[i], strict, memos)) {
          return false;
        }
      } else if (ObjectPrototypeHasOwnProperty(b, i)) {
        return false;
      } else {
        // Array is sparse.
        const keysA = ObjectKeys(a);
        for (; i < keysA.length; i++) {
          const key = keysA[i];
          if (!ObjectPrototypeHasOwnProperty(b, key) ||
              !innerDeepEqual(a[key], b[key], strict, memos)) {
            return false;
          }
        }
        if (keysA.length !== ObjectKeys(b).length) {
          return false;
        }
        return true;
      }
    }
  }
 
  // The pair must have equivalent values for every corresponding key.
  // Possibly expensive deep test:
  for (i = 0; i < keys.length; i++) {
    const key = keys[i];
    if (!innerDeepEqual(a[key], b[key], strict, memos)) {
      return false;
    }
  }
  return true;
}
 
function isDeepEqual(val1, val2) {
  return innerDeepEqual(val1, val2, kLoose);
}
 
function isDeepStrictEqual(val1, val2) {
  return innerDeepEqual(val1, val2, kStrict);
}
 
module.exports = {
  isDeepEqual,
  isDeepStrictEqual
};