improve duplicate detection in traverse function

Signed-off-by: Pedro Nastasi <pnastasi@expediagroup.com>
This commit is contained in:
Pedro Nastasi
2026-02-10 14:18:08 +00:00
parent 08a5813358
commit 013d19b30f
@@ -71,6 +71,11 @@ type Kv = {
// "h.j": "l"
export function traverse(root: unknown): Kv[] {
const output: Kv[] = [];
// Use a Set for O(1) case-insensitive duplicate detection of synthetic
// boolean path keys (e.g. "metadata.tags.java"), instead of the previous
// O(n) Array.some() linear scan which caused O(n²) overall complexity
// and severe event loop blocking for entities with large arrays.
const seenPathKeys = new Set<string>();
function visit(path: string, current: unknown) {
if (SPECIAL_KEYS.includes(path)) {
@@ -111,13 +116,9 @@ export function traverse(root: unknown): Kv[] {
visit(path, item);
if (typeof item === 'string') {
const pathKey = `${path}.${item}`;
if (
!output.some(
kv =>
kv.key.toLocaleLowerCase('en-US') ===
pathKey.toLocaleLowerCase('en-US'),
)
) {
const lowerKey = pathKey.toLocaleLowerCase('en-US');
if (!seenPathKeys.has(lowerKey)) {
seenPathKeys.add(lowerKey);
output.push({ key: pathKey, value: true });
}
}