Join GitHub today
GitHub is home to over 36 million developers working together to host and review code, manage projects, and build software together.Sign up
@wordpress/data: Introduce new custom `useDispatch` react hook #15896
useDispatch( storeName: ?string ): Object|Function
Also in this pull I've experimented with a
a. is it possible.
I have a working example in this pull and did some performance benchmarking with the following results:
Average time to load: 7834ms
Current Master (at time of writing this post)
Average time to load: 6161ms
Average time to load: 7730ms
With those results, I doubt we'll want to merge this with the
Things to do:
How has this been tested?
Types of changes
Performance metrics from the latest commit 7d88dd1:
Average time to load: 6410ms
I retested master because it seemed the performance tests were running slower on my machine and there was a bit of a difference from the original post:
Average time to load: 6831ms
So it actually looks like things are running a tad bit faster now :) The variations in metrics are not that significant so I think we can roll with this approach? Any agreement here? If so, I'll get this pull finished up with:
|Branch||avg load||avg dom load||avg type||slowest type||fastest type|
|useDispatch - with async||7246ms||6638ms||101ms||240ms||69ms|
|master - with async||6790ms||6208ms||98.92ms||227ms||56ms|
|useDispatch - no async||9190ms||7878ms||280.841ms||509ms||223ms|
|master - no async||9035ms||7590ms||293ms||631ms||219ms|
master is faster than this branch for the "with async" tests for the average but oddly with no async (which more closely tests the changes in
withDispatch), this branch is faster! On the whole this suggests that there are some marginal gains with the new
withDispatch and some minor negative impact on overall performance.
useMemo and without
useMemo performance tests.
The purpose of these tests (as suggested here was to see whether memoizing the returned action creators (via
useMemo) was slower or faster than the currently implemented logic. For the test, I created a mock component implementing
useDispatch with a mock registry and used
react-test-renderer to mount and unmount the component 1000 times. I tested invoking
useDispatch both with a store name and without the store name argument. The results are below:
|type||Avg time to render with store name||Avg time to render (no store name )|
It appears that the difference between the two is very marginal but there is an overhead cost (however minor) to implementing
useMemo so it appears we're good to leave the current logic as is.
Overall, from a performance perspective, I think there's nothing indicating that we need to backtrack on some of these changes. I'll continue on with covering all the changes in unit tests.