improve duplicate detection in traverse function
Signed-off-by: Pedro Nastasi <pnastasi@expediagroup.com>
This commit is contained in:
@@ -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 });
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user