Skip to content

Commit

Permalink
fix: query perf degradation from deep_copy util
Browse files Browse the repository at this point in the history
  • Loading branch information
tranhl committed Apr 14, 2024
1 parent 1e5b220 commit 8897130
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions packages/dynamoose/lib/utils/deep_copy.ts
@@ -1,6 +1,4 @@
import * as objectUtils from "js-object-utilities";

export default function deep_copy<T> (obj: T): T {
export default function deep_copy<T> (obj: T, refs = new Set()): T {
let copy: any;

// Handle Date
Expand All @@ -12,7 +10,7 @@ export default function deep_copy<T> (obj: T): T {

// Handle Array
if (obj instanceof Array) {
copy = obj.map((i) => deep_copy(i));
copy = obj.map((i) => deep_copy(i, refs));
return copy;
}

Expand Down Expand Up @@ -47,14 +45,28 @@ export default function deep_copy<T> (obj: T): T {

// Handle Object
if (obj instanceof Object) {
refs.add(obj);

if (obj.constructor !== Object) {
copy = Object.assign(Object.create(Object.getPrototypeOf(obj)), obj);
} else {
copy = {};
}

for (const attr in obj) {
if (Object.prototype.hasOwnProperty.call(obj, attr) && !objectUtils.isCircular(obj as any, attr)) {
copy[attr] = deep_copy(obj[attr]);
if (Object.prototype.hasOwnProperty.call(obj, attr)) {
const value = obj[attr];
const isObjValue = typeof value === "object" && !Array.isArray(value) && value !== null;

if (isObjValue) {
const isCircular = refs.has(value);
// Omit circular property from copy
if (isCircular) continue;

refs.add(value);
}

copy[attr] = deep_copy(obj[attr], refs);
}
}
return copy;
Expand Down

0 comments on commit 8897130

Please sign in to comment.