mirror of https://github.com/vuejs/core.git
perf(server-render): avoid unnecessary checks in `createBuffer` (#11364)
This commit is contained in:
parent
89e2d258dc
commit
fc205bf4de
|
@ -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()
|
||||||
|
},
|
||||||
|
},
|
||||||
|
)
|
||||||
|
})
|
|
@ -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.
|
||||||
|
|
Loading…
Reference in New Issue