perf(server-renderer): avoid materializing iterables in ssrRenderList#14821
perf(server-renderer): avoid materializing iterables in ssrRenderList#14821dennybiasiolli wants to merge 1 commit into
Conversation
Replace `Array.from(source)` with a `for...of` loop when iterating iterable sources in `ssrRenderList`. The previous code eagerly materialized the entire iterable into an intermediate array before rendering, which is unnecessary since `ssrRenderList` is void and does not need to collect results. The new approach iterates lazily, reducing memory overhead from O(n) to O(1) for the rendering loop itself. This avoids memory spikes when iterating large generators or custom iterables during SSR. Co-authored-by: Grok
📝 WalkthroughWalkthrough
ChangesLazy Iterable Rendering
🎯 2 (Simple) | ⏱️ ~12 minutes Suggested labels
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning Review ran into problems🔥 ProblemsGit: Failed to clone repository. Please run the Tip 💬 Introducing Slack Agent: The best way for teams to turn conversations into code.Slack Agent is built on CodeRabbit's deep understanding of your code, so your team can collaborate across the entire SDLC without losing context.
Built for teams:
One agent for your entire SDLC. Right inside Slack. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Size ReportBundles
Usages
|
@vue/compiler-core
@vue/compiler-dom
@vue/compiler-sfc
@vue/compiler-ssr
@vue/reactivity
@vue/runtime-core
@vue/runtime-dom
@vue/server-renderer
@vue/shared
vue
@vue/compat
commit: |
Summary
This PR removes an unnecessary intermediate array allocation in
ssrRenderListwhen iterating over iterable sources (generators,Set,Map, custom iterables).Problem
ssrRenderListhandlesv-foriteration during SSR. When the source is an object implementingSymbol.iterator, the current code callsArray.from(source)to eagerly materialize the entire iterable into an in-memory array before rendering any items:Since
ssrRenderListisvoid(it callsrenderItemfor side effects, not to collect results), the intermediate array is completely unnecessary. For large iterables (e.g., a generator yielding millions of items from a database cursor), this causes:renderItemcallback produces.Solution
Replace
Array.from(source)with afor...ofloop that lazily iterates the iterable and callsrenderItemfor each item as it is produced:This reduces the memory overhead of the rendering loop from O(n) to O(1), while producing identical output.
Why the client-side
renderListis not changedThe client-side
renderList(packages/runtime-core/src/helpers/renderList.ts) already uses the two-argument formArray.from(source, mapFn), which maps during iteration in a single pass without creating an intermediate array. Since it must return aVNodeChild[], it needs the result array regardless — there is no unnecessary allocation to eliminate.Test Plan
packages/server-renderer/__tests__/ssrRenderList.spec.ts:Setiteration — verifies items and indices are correct forSetsources.Mapiteration — verifies items and indices are correct forMapsources.Co-authored-by: Grok
Summary by CodeRabbit
Release Notes