Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(lib-dynamodb): reduce object copying in iterators #4194

Merged
merged 1 commit into from
Nov 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,23 +35,19 @@ const processKeyInObj = (obj: any, processFunc: Function, children?: KeyNode[] |
return processObj(obj, processFunc, children);
};

const processKeysInObj = (obj: any, processFunc: Function, keyNodes: KeyNode[]) =>
keyNodes.reduce(
(acc, { key, children }) => ({
...acc,
[key]: processKeyInObj(acc[key], processFunc, children),
}),
obj
);
const processKeysInObj = (obj: any, processFunc: Function, keyNodes: KeyNode[]) => {
const accumulator = { ...obj };
return keyNodes.reduce((acc, { key, children }) => {
acc[key] = processKeyInObj(acc[key], processFunc, children);
return acc;
}, accumulator);
};

const processAllKeysInObj = (obj: any, processFunc: Function, children?: KeyNode[] | AllNodes): any =>
Object.entries(obj).reduce(
(acc, [key, value]) => ({
...acc,
[key]: processKeyInObj(value, processFunc, children),
}),
{}
);
Object.entries(obj).reduce((acc, [key, value]) => {
acc[key] = processKeyInObj(value, processFunc, children);
return acc;
}, {} as any);

export const marshallInput = (obj: any, keyNodes: KeyNode[], options?: marshallOptions) => {
const marshallFunc = (toMarshall: any) => marshall(toMarshall, options);
Expand Down
26 changes: 11 additions & 15 deletions lib/lib-dynamodb/src/commands/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,19 @@ const processKeyInObj = (obj: any, processFunc: Function, children?: KeyNode[] |
return processObj(obj, processFunc, children);
};

const processKeysInObj = (obj: any, processFunc: Function, keyNodes: KeyNode[]) =>
keyNodes.reduce(
(acc, { key, children }) => ({
...acc,
[key]: processKeyInObj(acc[key], processFunc, children),
}),
obj
);
const processKeysInObj = (obj: any, processFunc: Function, keyNodes: KeyNode[]) => {
const accumulator = { ...obj };
return keyNodes.reduce((acc, { key, children }) => {
acc[key] = processKeyInObj(acc[key], processFunc, children);
return acc;
}, accumulator);
};

const processAllKeysInObj = (obj: any, processFunc: Function, children?: KeyNode[] | AllNodes): any =>
Object.entries(obj).reduce(
(acc, [key, value]) => ({
...acc,
[key]: processKeyInObj(value, processFunc, children),
}),
{}
);
Object.entries(obj).reduce((acc, [key, value]) => {
acc[key] = processKeyInObj(value, processFunc, children);
return acc;
}, {} as any);

export const marshallInput = (obj: any, keyNodes: KeyNode[], options?: marshallOptions) => {
const marshallFunc = (toMarshall: any) => marshall(toMarshall, options);
Expand Down
12 changes: 12 additions & 0 deletions packages/util-dynamodb/src/convertToNative.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,18 @@ describe("convertToNative", () => {
const output = { numberKey: { value: "1.01" }, bigintKey: { value: "9007199254740996" } };
expect(convertToNative({ M: input }, { wrapNumbers: true })).toEqual(output);
});

it(`testing map with big objects`, () => {
const input = Array.from({ length: 100000 }, (_, idx) => [idx, { N: "1.00" }]).reduce((acc, [key, value]) => {
acc[key as unknown as string] = value;
return acc;
}, {});
const output = Array.from({ length: 100000 }, (_, idx) => [idx, 1]).reduce((acc, [key, value]) => {
acc[key as unknown as string] = value;
return acc;
}, {});
expect(convertToNative({ M: input })).toEqual(output);
});
});

describe("set", () => {
Expand Down
7 changes: 3 additions & 4 deletions packages/util-dynamodb/src/convertToNative.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ const convertMap = (
options?: unmarshallOptions
): Record<string, NativeAttributeValue> =>
Object.entries(map).reduce(
(acc: Record<string, NativeAttributeValue>, [key, value]: [string, AttributeValue]) => ({
...acc,
[key]: convertToNative(value, options),
}),
(acc: Record<string, NativeAttributeValue>, [key, value]: [string, AttributeValue]) => (
(acc[key] = convertToNative(value, options)), acc
),
{}
);