Skip to content

Commit 0b4c00e

Browse files
Bring checking isArray out of the loop (#14)
1 parent c8efe46 commit 0b4c00e

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

index.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,22 @@ interface Difference {
66
interface Options {
77
cyclesFix: boolean;
88
}
9+
910
const t = true;
1011
const richTypes = { Date: t, RegExp: t, String: t, Number: t };
12+
1113
export default function diff(
1214
obj: Record<string, any> | any[],
1315
newObj: Record<string, any> | any[],
1416
options: Partial<Options> = { cyclesFix: true },
1517
_stack: Record<string, any>[] = []
1618
): Difference[] {
1719
let diffs: Difference[] = [];
20+
const isObjArray = Array.isArray(obj);
21+
1822
for (const key in obj) {
1923
const objKey = obj[key];
20-
const path = Array.isArray(obj) ? +key : key;
24+
const path = isObjArray ? +key : key;
2125
if (!(key in newObj)) {
2226
diffs.push({
2327
type: "REMOVE",
@@ -64,11 +68,13 @@ export default function diff(
6468
});
6569
}
6670
}
71+
72+
const isNewObjArray = Array.isArray(newObj);
6773
for (const key in newObj) {
6874
if (!(key in obj)) {
6975
diffs.push({
7076
type: "CREATE",
71-
path: [Array.isArray(newObj) ? +key : key],
77+
path: [isNewObjArray ? +key : key],
7278
value: newObj[key],
7379
});
7480
}

0 commit comments

Comments
 (0)