code: function collectDuplicates(value) {
      if (value == null || typeof value !== 'object') {
        return;
      }
      const metadata = metadataForVal.get(value);
      // Only consider duplicates with hashes longer than 2 (excludes [] and {}).
      if (metadata && metadata.value !== value && metadata.hash.length > 2) {
        metadata.isDuplicate = true;
        return;
      }
      if (Array.isArray(value)) {
        for (let i = 0; i < value.length; i++) {
          collectDuplicates(value[i]);
        }
      } else {
        for (const k in value) {
          if (value.hasOwnProperty(k) && value[k] !== undefined) {
            collectDuplicates(value[k]);
          }
        }
      }
    }
docs: Using top-down recursion, linearly scan the JSON tree to determine which values should be deduplicated.