Skip to content

Commit

Permalink
perf(server-render): avoid unnecessary checks in createBuffer (#11364)
Browse files Browse the repository at this point in the history
  • Loading branch information
negezor authored Jul 15, 2024
1 parent 89e2d25 commit fc205bf
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 2 deletions.
65 changes: 65 additions & 0 deletions packages/server-renderer/__tests__/createBuffer.bench.ts
Original file line number Diff line number Diff line change
@@ -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()
},
},
)
})
4 changes: 2 additions & 2 deletions packages/server-renderer/src/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ export function createBuffer() {
const isStringItem = isString(item)
if (appendable && isStringItem) {
buffer[buffer.length - 1] += item as string
} else {
buffer.push(item)
return
}
buffer.push(item)
appendable = isStringItem
if (isPromise(item) || (isArray(item) && item.hasAsync)) {
// promise, or child buffer with async, mark as async.
Expand Down

0 comments on commit fc205bf

Please sign in to comment.