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.
This PR adds the
createRouterMatcherSortCache
function which accepts a sortedRouterRecord
array (returned fromrouter.getRoutes()
and returns a map of keys and sort index. This can be passed asrestoreCache
option tocreateRouter
, if this cache is provided the path sorting is skipped when adding routes during matcher creation and the sort cache is used to restore the order after all initial routes have been added.The sort cache and restoration is not perfect, it's more of a proof of concept, I tried to add this functionality without making any breaking changes.
The usage would be as follows:
So far with limited testing the cache may have issues with 'duplicate' routes, there may not be enough information from records returned from
getRoutes
to create a fully unique key, though so far these duplicates have the same score and I don't know if the order between these have any significance. I think if the router matcher were exposed, or the matchers array directly (something likegetMatchers
), it may be possible to more accurately create a sort cache, I opted not to this in this iteration to keep changes to a minimum.I have added a branch to my performance comparison which also tests router creation with cached sort [here].(https://github.com/BobbieGoede/vue-router-perf-bench/tree/feature/sort-cache-comparison). Just caching the order of the matchers greatly reduces the creation time, results on my machine:
This is just one of the ways router performance could be improved, I will still look into what it would like to support custom matchers and perhaps more accurate ways of precompiling the matcher. Maybe there are obvious flaws in this implementation, I'm not attached to this implementation so please let me know 😄!