-
Notifications
You must be signed in to change notification settings - Fork 405
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(store): avoid going over states list every time action is dispat…
…ched (#2219) In this commit, I'm working on optimizing how we handle action dispatches by avoiding repeated traversal of the `states` list. Instead, we prepare a map each time a new state is added, allowing us to perform O(1) lookups by action type in the future. This approach reduces complexity and improves performance. I've tested it with benchmark.js, and here are the results: ``` class Increment { static readonly type = 'Increment'; } const states = Array.from({ length: 50 }).map((_, index) => { @State({ name: `counter_${index + 1}`, defaults: 0, }) @Injectable() class CounterState { @action(Increment) increment(ctx: StateContext<number>) { ctx.setState((counter) => counter + 1); } } return CounterState; }); store.dispatch() before changes x 3,435 ops/sec ±0.45% (65 runs sampled) store.dispatch() after changes x 3,942 ops/sec ±1.21% (25 runs sampled) ```
- Loading branch information
Showing
5 changed files
with
107 additions
and
63 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