Link Performance #2011
Replies: 2 comments 3 replies
-
This isn't really a "solution" but I've basically solved the problem in my case by building a severely cut down version of the link component that does exactly what I need in this case. /**
* Faster version of the link component that only works with static routes (no params, search, etc). By avoiding this check,
* the render time is much quicker
*/
const FastLink = ({ children, to }) => {
/***** HOOKS *****/
const navigate = useNavigate();
/***** RENDER *****/
return (
<a
href={to}
onClick={(e) => {
e.preventDefault();
navigate({ to });
}}
>
{children}
</a>
);
}; I've noticed calling Perhaps having a select function on useRouter would solve this? For now this handles my use case, but I think some thought needs to be put into this because Link components are, in my opinion, now considered an "expensive" component despite being a foundational component in most applications. |
Beta Was this translation helpful? Give feedback.
-
We've observed the same issue. The time goes up dramatically when you have hundreds of registered routes and render hundreds of Links at once. To reproduce this issue, I've used the large-file-based example as a base and rendered 10000 links with random IDs as a param. Then, I replaced the built-in Link component with Here's a comparison table with times to render taken from react dev tools:
None of the computations in @SeanCassiere Would you know where to look? 🙏 |
Beta Was this translation helpful? Give feedback.
-
In my companies application, we have a support centre page where we show our support articles. When we render the links, we render these are nested categories, sub categories and articles (articles being basically just a link element to a page that renders the actual article).
Recently having converted this particular page from react router v5 to tanstack, I'm noticing a significant delay in rendering time. For example, previously it was taking about 15ms to render the page and now it's easily over 200ms - not ideal.
Having done some investigation, it looks like each Link component on average is taking between 0.3 and 0.6ms to render which on this particular page, adds up to a lot since we render (in some cases) 300-500 links.
It doesn't help that we have hundreds of support articles on each page, so when we click an accordion, even if the existing links are optimized it still takes a long time to render because new links are being rendered.
Converting this directly to anchor elements seems to immediately resolve any performance issues.
It'd be great if this list was paginated, virtualized, etc, but it's really not something we can afford to be spending time on at the moment while this refactor is purely meant to be the routing side of things.
So I'm curious of any suggestions on how to actually improve the page performance apart from refactoring the entire page/structure.
Also, this performance issue can be seen easily by chucking this in the rootRoute of any of the example docs and checking the console which should immediately complain about the page taking too long to render
I also pulled tanstack router locally and tried logging different points in the Link component to see if anything else was slowing it down. Other than the context consistently taking 0.2ms to run, there wasn't anything that stood out to me.
--- Edit ---
It looks like the buildLocation function takes a long time to run during render time (relative) which adds up with hundreds of links. Building a custom Link component without the call to buildLocation resolves performance issues but means that we cannot have type safety so it's not ideal.
Beta Was this translation helpful? Give feedback.
All reactions