forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup spread operators in
reduce
calls (elastic#157471)
## Summary The spread operator is costly and put pressure on GC. It should be avoided when possible, especially in loops. This PR adapts a lot of `reduce` calls in the codebase to remove the usages of the diabolic spread operator, when possible. Note: the PR is not fully exhaustive. I focused on the server-side, as we're more directly impacted than on browser-side code regarding performances. ## Removing `...` usages in `kittens.reduce()` For `reduce` loops, the spread operator can usually easily be replaced: #### - setting a value on the accum object and returning it #### BAD ```ts return this.toArray().reduce( (acc, renderer) => ({ ...acc, [renderer.name]: renderer, }), {} as Record<string, ExpressionRenderer> ); ``` #### GOOD ```ts return this.toArray().reduce((acc, renderer) => { acc[renderer.name] = renderer; return acc; }, {} as Record<string, ExpressionRenderer>); ``` #### - assigning values to the accum object and returning it #### BAD ```ts const allAggs: Record<string, any> = fieldAggRequests.reduce( (aggs: Record<string, any>, fieldAggRequest: unknown | null) => { return fieldAggRequest ? { ...aggs, ...(fieldAggRequest as Record<string, any>) } : aggs; }, {} ); ``` #### GOOD ```ts const allAggs = fieldAggRequests.reduce<Record<string, any>>( (aggs: Record<string, any>, fieldAggRequest: unknown | null) => { if (fieldAggRequest) { Object.assign(aggs, fieldAggRequest); } return aggs; }, {} ); ``` #### - pushing items to the accum list and returning it #### BAD ```ts const charsFound = charToArray.reduce( (acc, char) => (value.includes(char) ? [...acc, char] : acc), [] as string[] ); ``` #### GOOD ```ts const charsFound = charToArray.reduce((acc, char) => { if (value.includes(char)) { acc.push(char); } return acc; }, [] as string[]); ``` ## Questions #### Are you sure all the changes in this are strictly better for runtime performances? Yes, yes I am. #### How much better? Likely not much. #### Are you planning on analyzing the perf gain? Nope. #### So why did you do it? I got tired of seeing badly used spread operators in my team's owned code, and I had some extra time during on-week, so I spent a few hours adapting the usages in all our runtime/production codebase. #### Was it fun? Take your best guess. --------- Co-authored-by: Kibana Machine <[email protected]>
- Loading branch information
Showing
132 changed files
with
690 additions
and
773 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.