perf(server-render): avoid unnecessary checks in `createBuffer` (#11364)

This commit is contained in:
Vlad 2024-07-16 01:31:27 +11:00 committed by GitHub
parent 89e2d258dc
commit fc205bf4de
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 67 additions and 2 deletions

View File

@ -0,0 +1,65 @@
import { bench, describe } from 'vitest'
import { createBuffer } from '../src/render'
describe('createBuffer', () => {
let stringBuffer = createBuffer()
bench(
'string only',
() => {
for (let i = 0; i < 10; i += 1) {
stringBuffer.push('hello')
}
},
{
setup() {
stringBuffer = createBuffer()
},
},
)
let stringNestedBuffer = createBuffer()
bench(
'string with nested',
() => {
for (let i = 0; i < 10; i += 1) {
if (i % 3 === 0) {
stringNestedBuffer.push('hello')
} else {
const buffer = createBuffer()
buffer.push('hello')
stringNestedBuffer.push(buffer.getBuffer())
}
}
},
{
setup() {
stringNestedBuffer = createBuffer()
},
},
)
bench(
'string with nested async',
() => {
for (let i = 0; i < 10; i += 1) {
if (i % 3 === 0) {
const buffer = createBuffer()
buffer.push('hello')
stringNestedBuffer.push(Promise.resolve(buffer.getBuffer()))
} else {
const buffer = createBuffer()
buffer.push('hello')
stringNestedBuffer.push(buffer.getBuffer())
}
}
},
{
setup() {
stringNestedBuffer = createBuffer()
},
},
)
})

View File

@ -73,9 +73,9 @@ export function createBuffer() {
const isStringItem = isString(item) const isStringItem = isString(item)
if (appendable && isStringItem) { if (appendable && isStringItem) {
buffer[buffer.length - 1] += item as string buffer[buffer.length - 1] += item as string
} else { return
buffer.push(item)
} }
buffer.push(item)
appendable = isStringItem appendable = isStringItem
if (isPromise(item) || (isArray(item) && item.hasAsync)) { if (isPromise(item) || (isArray(item) && item.hasAsync)) {
// promise, or child buffer with async, mark as async. // promise, or child buffer with async, mark as async.