Skip to content

[BUG] [typescript-fetch] mapValues in runtime has O(n^2) complexity #21046

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

Closed
david-aeqium opened this issue Apr 7, 2025 · 0 comments · Fixed by #21047
Closed

[BUG] [typescript-fetch] mapValues in runtime has O(n^2) complexity #21046

david-aeqium opened this issue Apr 7, 2025 · 0 comments · Fixed by #21047

Comments

@david-aeqium
Copy link
Contributor

The implementation of mapValues in the typescript-fetch runtime currently takes O(n^2) time relative to the number of keys (n) in the data object. This is due to the use of the spread operator ... within the reduce callback, which copies the entire accumulated object on every iteration.

Problem code:
https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-fetch/runtime.mustache#L335-L340

  return Object.keys(data).reduce(
    (acc, key) => ({ ...acc, [key]: fn(data[key]) }),
    {}
  );

Impact:
This inefficiency can lead to noticeable performance degradation in generated clients.

Proposed solution:
Use a simple loop with direct assignment to improve the performance. I will put up a PR shortly with the proposed fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant