Skip to content

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

@david-aeqium

Description

@david-aeqium

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions