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

fix(all): be more lenient, reduce memory usage #306

Merged
merged 2 commits into from
Nov 12, 2024
Merged

Conversation

aleclarson
Copy link
Member

@aleclarson aleclarson commented Nov 12, 2024

Tip

The owner of this PR can publish a preview release by commenting /publish in this PR. Afterwards, anyone can try it out by running pnpm add radashi@pr<PR_NUMBER>.

Summary

Make all less strict both at the type level and at runtime, making it closer to Promise.all behavior.

  1. Values are not required to have a then method (e.g. promises and promise-like objects). If no then method exists, the value is used as-is.
  2. I've made the following performance improvements:
  • Avoid heap allocations where possible. Previously, all would create one array per promise when called with an array of promises. In all cases, all would create an additional object upon the resolution of each promise. Upon completion, all would filter the results, which allocated another array, and map the filtered results afterwards, allocating yet another array. For each promise, all would allocate two functions (a then callback and a catch callback).
  • Stop creating an empty object {} and using Array#reduce to populate it. Instead, use spread syntax on the input object, thereby reusing the input object's V8 “hidden class”. Then gradually assign the resolved values to the output object.

Related issue, if any:

For any code change,

  • Related documentation has been updated, if needed
  • Related tests have been added or updated, if needed
  • Related benchmarks have been added or updated, if needed
  • Release notes in next-minor.md or next-major.md have been added, if needed

Does this PR introduce a breaking change?

No

Bundle impact

Status File Size 1 Difference
M src/async/all.ts 559 -66 (-11%)

Footnotes

  1. Function size includes the import dependencies of the function.

@aleclarson aleclarson changed the title fix(all): better types and perf improvements fix(all): be more lenient, reduce memory usage Nov 12, 2024
@aleclarson aleclarson merged commit 221cac2 into main Nov 12, 2024
7 checks passed
@aleclarson aleclarson deleted the fix/all-signature branch November 12, 2024 18:02
@radashi-bot
Copy link

A stable release 12.2.3 has been published to NPM. 🚀

To install:

See the changes

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

Successfully merging this pull request may close these issues.

2 participants