-
Notifications
You must be signed in to change notification settings - Fork 403
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
perf(store): avoid going over states list every time action is dispatched #2219
Conversation
☁️ Nx Cloud ReportCI is running/has finished running commands for commit 2bf876b. As they complete they will appear below. Click to see the status, the terminal output, and the build insights. 📂 See all runs for this CI Pipeline Execution ✅ Successfully ran 4 targetsSent with 💌 from NxCloud. |
commit: |
BundleMonUnchanged files (6)
No change in files bundle size Unchanged groups (2)
Final result: ✅ View report in BundleMon website ➡️ |
BundleMon (NGXS Plugins)Unchanged files (9)
No change in files bundle size Unchanged groups (1)
Final result: ✅ View report in BundleMon website ➡️ |
BundleMon (Integration Projects)Unchanged files (3)
No change in files bundle size Final result: ✅ View report in BundleMon website ➡️ |
48afa41
to
8a7cca6
Compare
Code Climate has analyzed commit 8a7cca6 and detected 0 issues on this pull request. The test coverage on the diff in this pull request is 100.0% (50% is the threshold). This pull request will bring the total coverage in the repository to 95.3% (0.0% change). View more on Code Climate. |
8a7cca6
to
927ea8c
Compare
…ched 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) ```
927ea8c
to
c1c8437
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great improvement! I would love to know the performance change when you have 100 states and 10 actions each, and fire each of the 1000 actions.
PS. I would like to chat with you about the direction I want to go with the action handler registration, because I think that it will bring much improvement to the code.
You just need to update the max bundle size for the main bundle. Currently it is 100kb, but you can change it to 101kb to make this pass |
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: