Open History to see the last commit
1import { bench, describe } from 'vitest'
2β
3const MAX_DYNAMIC_RECORD_CODE_UNITS = 64 * 1024
4const SOURCE_SEPARATOR = ';'
5const SCRIPT_OPENING_CODE_UNITS = '<script>'.length
6const DYNAMIC_CLOSE_CODE_UNITS = 'document.currentScript.remove()</script>'
7 .length
8β
9function getBatchLength(
10 values: ReadonlyArray<string | undefined>,
11 head: number,
12) {
13 let codeUnits = SCRIPT_OPENING_CODE_UNITS + DYNAMIC_CLOSE_CODE_UNITS
14 let batchLength = 0
15 for (let index = head; index < values.length; index++) {
16 const nextCodeUnits =
17 codeUnits + SOURCE_SEPARATOR.length + values[index]!.length
18 if (batchLength > 0 && nextCodeUnits > MAX_DYNAMIC_RECORD_CODE_UNITS) {
19 break
20 }
21 codeUnits = nextCodeUnits
22 batchLength++
23 if (codeUnits > MAX_DYNAMIC_RECORD_CODE_UNITS) {
24 break
25 }
26 }
27 return batchLength
28}
29β
30function drainWithSplice(values: Array<string>) {
31 let drained = 0
32 let consumedCodeUnits = 0
33 while (values.length > 0) {
34 const batchLength = getBatchLength(values, 0)
35 const batch = values.splice(0, batchLength)
36 drained += batchLength
37 for (const part of batch) {
38 consumedCodeUnits += part.length
39 }
40 }
41 return { drained, consumedCodeUnits }
42}
43β
44function drainWithHead(values: Array<string | undefined>) {
45 let head = 0
46 let drained = 0
47 let consumedCodeUnits = 0
48 while (head < values.length) {
49 const batchLength = getBatchLength(values, head)
50 if (head === 0 && batchLength === values.length) {
51 const batch = values
52 drained += values.length
53 for (const part of batch) {
54 consumedCodeUnits += part!.length
55 }
56 values = []
57 head = 0
58 continue
59 }
60 const end = head + batchLength
61 const batch = values.slice(head, end)
62 for (let index = head; index < end; index++) {
63 values[index] = undefined
64 }
65 drained += batch.length
66 for (const part of batch) {
67 consumedCodeUnits += part!.length
68 }
69 head = end
70 if (head === values.length) {
71 values = []
72 head = 0
73 } else if (head >= 1024 && head >= values.length - head) {
74 values = values.slice(head)
75 head = 0
76 }
77 }
78 return { drained, consumedCodeUnits }
79}
80β
81const source = 'x'.repeat(4 * 1024)
82const sources = Array.from({ length: 4_095 }, () => source)
83let benchmarkSink: { drained: number; consumedCodeUnits: number } | undefined
84β
85const expected = drainWithSplice(sources.slice())
86const actual = drainWithHead(sources.slice())
87if (
88 expected.drained !== sources.length ||
89 actual.drained !== sources.length ||
90 actual.consumedCodeUnits !== expected.consumedCodeUnits
91) {
92 throw new Error('Hydration queue benchmark discarded a source')
93}
94β
95describe('maximum hydration source queue with 4 KiB sources', () => {
96 bench('front splice', () => {
97 return void (benchmarkSink = drainWithSplice(sources.slice()))
98 })
99β
100 bench('head index with compaction', () => {
101 return void (benchmarkSink = drainWithHead(sources.slice()))
102 })
103})