perf(server-render): avoid unnecessary checks in createBuffer
#11364
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.
Early return in
buffer.push()
forappendable
yields almost a twofold increase in speed, as we no longer need to check forPromise
and other buffers, nor do we need to reassignappendable
.Performance was tested on the following hardware:
CPU:
AMD Ryzen 7950x3D
System:
WSL 2 Arch Linux on Windows 11
Node.js:
v22.4.0
Before:
After:
I also conducted some research to understand where most of the time is spent when working with the buffer in several different applications. For testing, I had two medium-sized Nuxt 3 applications and one large project with its own SSR. The results were quite similar; I'll provide an example from the latter. I went through all the pages once with a variant that completely changes the content, and here are the results:
What is the first element in the buffer
When all elements in the buffer are of the same type
Total number of different elements
I also considered changing the initial buffer behavior based on this data by setting
appendable: true
and initializing the buffer with an empty stringbuffer = ['']
by default. However, I couldn't get consistent measurements, as the results vary significantly. Do you have any thoughts on this?