fix(all): be more lenient, reduce memory usage #306
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Tip
The owner of this PR can publish a preview release by commenting
/publish
in this PR. Afterwards, anyone can try it out by runningpnpm add radashi@pr<PR_NUMBER>
.Summary
Make
all
less strict both at the type level and at runtime, making it closer toPromise.all
behavior.then
method (e.g. promises and promise-like objects). If nothen
method exists, the value is used as-is.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 (athen
callback and acatch
callback).{}
and usingArray#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,
Does this PR introduce a breaking change?
No
Bundle impact
src/async/all.ts
Footnotes
Function size includes the
import
dependencies of the function. ↩