Skip to content

Commit 71d8b0e

Browse files
test(helpers): optimize decycle to for...of for performance
1 parent de7a775 commit 71d8b0e

1 file changed

Lines changed: 13 additions & 13 deletions

File tree

__tests__/helpers/decycle.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,25 @@ function deepCopy(
9292
visitedObjects.set(value, path);
9393

9494
if (Array.isArray(value)) {
95-
return value.map((element, index) =>
96-
deepCopy(
97-
element,
98-
`${path}[${index.toString()}]`,
99-
visitedObjects,
100-
replacer,
101-
),
102-
);
95+
const copy: unknown[] = [];
96+
97+
for (const [index, element] of value.entries()) {
98+
const newPath = `${path}[${index.toString()}]`;
99+
copy[index] = deepCopy(element, newPath, visitedObjects, replacer);
100+
}
101+
102+
return copy;
103103
}
104104

105105
const record = value as Record<string, unknown>;
106+
const copy: DecycledObject = {};
106107

107-
return Object.keys(record).reduce<DecycledObject>((copy, key) => {
108-
const value = record[key];
108+
for (const key of Object.keys(record)) {
109109
const newPath = `${path}[${JSON.stringify(key)}]`;
110+
copy[key] = deepCopy(record[key], newPath, visitedObjects, replacer);
111+
}
110112

111-
copy[key] = deepCopy(value, newPath, visitedObjects, replacer);
112-
return copy;
113-
}, {});
113+
return copy;
114114
}
115115

116116
/**

0 commit comments

Comments
 (0)