optimized pick() to eliminate intermediate array allocation#636
optimized pick() to eliminate intermediate array allocation#636khresth wants to merge 2 commits intomakenotion:mainfrom
Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes the internal pick() helper in src/utils.ts to reduce per-call allocations/iteration overhead by replacing the keys.map() + Object.fromEntries() approach with a single-loop object construction. This supports overall SDK request performance since pick() is used when building request query/body params.
Changes:
- Reimplemented
pick()to build the result object via afor...ofloop instead of creating intermediate[key, value]tuples. - Introduced a direct assignment-based implementation returning the accumulated object.
| const result = {} as Pick<O, K> | ||
| for (const key of keys) { | ||
| result[key] = (base as any)?.[key] | ||
| } |
There was a problem hiding this comment.
pick() now uses (base as any)?.[key], introducing an any escape hatch that removes type safety and can mask real type errors. Consider restructuring the types so the loop can assign without any (e.g., build into a Partial<Pick<...>>/record shape and cast once at the end, or adjust the base parameter type to explicitly allow nullish values and keep the indexed access typed).
There was a problem hiding this comment.
^ Looks like a build error unfortunately, Error: 20:28 error Unexpected any. Specify a different type @typescript-eslint/no-explicit-any
Description
Optimized the
pick()function to improve performance by eliminating intermediate array allocations. The original implementation usedkeys.map()to create an array of[key, value]tuples, then converted it to an object viaObject.fromEntries(). This created temporary data structures on every invocation.The new implementation directly constructs the result object in a single loop, reducing memory allocations and iteration overhead. Since
pick()is called twice per API request (for query params and body params), this optimization should improve performance across SDK operations.How was this change tested?
Test Results:
PASS test/id-extraction.test.ts
PASS test/helpers.test.ts
PASS test/Client.test.ts
Test Suites: 3 passed, 3 total
Tests: 24 passed, 24 total
Screenshots