-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(server-renderer): optimize
unrollBuffer
by avoiding promises (#…
- Loading branch information
Showing
2 changed files
with
112 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { bench, describe } from 'vitest' | ||
|
||
import { type SSRBuffer, createBuffer } from '../src/render' | ||
import { unrollBuffer } from '../src/renderToString' | ||
|
||
function createSyncBuffer(levels: number, itemsPerLevel: number): SSRBuffer { | ||
const buffer = createBuffer() | ||
|
||
function addItems(buf: ReturnType<typeof createBuffer>, level: number) { | ||
for (let i = 1; i <= levels * itemsPerLevel; i++) { | ||
buf.push(`sync${level}.${i}`) | ||
} | ||
if (level < levels) { | ||
const subBuffer = createBuffer() | ||
addItems(subBuffer, level + 1) | ||
buf.push(subBuffer.getBuffer()) | ||
} | ||
} | ||
|
||
addItems(buffer, 1) | ||
return buffer.getBuffer() | ||
} | ||
|
||
function createMixedBuffer(levels: number, itemsPerLevel: number): SSRBuffer { | ||
const buffer = createBuffer() | ||
|
||
function addItems(buf: ReturnType<typeof createBuffer>, level: number) { | ||
for (let i = 1; i <= levels * itemsPerLevel; i++) { | ||
if (i % 3 === 0) { | ||
// @ts-expect-error testing... | ||
buf.push(Promise.resolve(`async${level}.${i}`)) | ||
} else { | ||
buf.push(`sync${level}.${i}`) | ||
} | ||
} | ||
if (level < levels) { | ||
const subBuffer = createBuffer() | ||
addItems(subBuffer, level + 1) | ||
buf.push(subBuffer.getBuffer()) | ||
} | ||
} | ||
|
||
addItems(buffer, 1) | ||
return buffer.getBuffer() | ||
} | ||
|
||
describe('unrollBuffer', () => { | ||
let syncBuffer = createBuffer().getBuffer() | ||
let mixedBuffer = createBuffer().getBuffer() | ||
|
||
bench( | ||
'sync', | ||
() => { | ||
return unrollBuffer(syncBuffer) as any | ||
}, | ||
{ | ||
setup() { | ||
syncBuffer = createSyncBuffer(5, 3) | ||
}, | ||
}, | ||
) | ||
|
||
bench( | ||
'mixed', | ||
() => { | ||
return unrollBuffer(mixedBuffer) as any | ||
}, | ||
{ | ||
setup() { | ||
mixedBuffer = createMixedBuffer(5, 3) | ||
}, | ||
}, | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters