Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 6791c88

Browse files
authored
perf: improve benchmark test code (#268)
1 parent 95e93c1 commit 6791c88

File tree

3 files changed

+42
-22
lines changed

3 files changed

+42
-22
lines changed

benchmark/client/App.vue

Lines changed: 33 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
<script setup lang="ts" vapor>
2-
import { ref, shallowRef } from '@vue/vapor'
2+
import {
3+
ref,
4+
shallowRef,
5+
triggerRef,
6+
watch,
7+
type ShallowRef,
8+
type WatchSource,
9+
} from '@vue/vapor'
310
import { buildData } from './data'
411
import { defer, wrap } from './profiling'
512
@@ -9,50 +16,47 @@ const selected = ref<number>()
916
const rows = shallowRef<
1017
{
1118
id: number
12-
label: string
19+
label: ShallowRef<string>
1320
}[]
1421
>([])
1522
16-
function setRows(update = rows.value.slice()) {
17-
rows.value = update
18-
}
19-
23+
// Bench Add: https://jsbench.me/45lzxprzmu/1
2024
const add = wrap('add', () => {
21-
rows.value = rows.value.concat(buildData(1000))
25+
rows.value.push(...buildData(1000))
26+
triggerRef(rows)
2227
})
2328
2429
const remove = wrap('remove', (id: number) => {
2530
rows.value.splice(
2631
rows.value.findIndex(d => d.id === id),
2732
1,
2833
)
29-
setRows()
34+
triggerRef(rows)
3035
})
3136
3237
const select = wrap('select', (id: number) => {
3338
selected.value = id
3439
})
3540
3641
const run = wrap('run', () => {
37-
setRows(buildData())
42+
rows.value = buildData()
3843
selected.value = undefined
3944
})
4045
4146
const update = wrap('update', () => {
4247
const _rows = rows.value
43-
for (let i = 0; i < _rows.length; i += 10) {
44-
_rows[i].label += ' !!!'
48+
for (let i = 0, len = _rows.length; i < len; i += 10) {
49+
_rows[i].label.value += ' !!!'
4550
}
46-
setRows()
4751
})
4852
4953
const runLots = wrap('runLots', () => {
50-
setRows(buildData(10000))
54+
rows.value = buildData(10000)
5155
selected.value = undefined
5256
})
5357
5458
const clear = wrap('clear', () => {
55-
setRows([])
59+
rows.value = []
5660
selected.value = undefined
5761
})
5862
@@ -63,7 +67,7 @@ const swapRows = wrap('swap', () => {
6367
const d998 = _rows[998]
6468
_rows[1] = d998
6569
_rows[998] = d1
66-
setRows()
70+
triggerRef(rows)
6771
}
6872
})
6973
@@ -74,6 +78,18 @@ async function bench() {
7478
await runLots()
7579
}
7680
}
81+
82+
// Reduce the complexity of `selected` from O(n) to O(1).
83+
function createSelector(source: WatchSource) {
84+
const cache: Record<keyof any, ShallowRef<boolean>> = {}
85+
watch(source, (val, old) => {
86+
if (old != undefined) cache[old]!.value = false
87+
if (val != undefined) cache[val]!.value = true
88+
})
89+
return (id: keyof any) => (cache[id] ??= shallowRef(false)).value
90+
}
91+
92+
const isSelected = createSelector(selected)
7793
</script>
7894

7995
<template>
@@ -96,11 +112,11 @@ async function bench() {
96112
<tr
97113
v-for="row of rows"
98114
:key="row.id"
99-
:class="{ danger: row.id === selected }"
115+
:class="{ danger: isSelected(row.id) }"
100116
>
101117
<td>{{ row.id }}</td>
102118
<td>
103-
<a @click="select(row.id)">{{ row.label }}</a>
119+
<a @click="select(row.id)">{{ row.label.value }}</a>
104120
</td>
105121
<td>
106122
<button @click="remove(row.id)">x</button>

benchmark/client/data.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { shallowRef } from '@vue/vapor'
2+
13
let ID = 1
24

35
function _random(max: number) {
@@ -64,12 +66,13 @@ export function buildData(count = 1000) {
6466
for (let i = 0; i < count; i++)
6567
data.push({
6668
id: ID++,
67-
label:
69+
label: shallowRef(
6870
adjectives[_random(adjectives.length)] +
69-
' ' +
70-
colours[_random(colours.length)] +
71-
' ' +
72-
nouns[_random(nouns.length)],
71+
' ' +
72+
colours[_random(colours.length)] +
73+
' ' +
74+
nouns[_random(nouns.length)],
75+
),
7376
})
7477
return data
7578
}

packages/runtime-vapor/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export const version: string = __VERSION__
44
export {
55
// core
66
type Ref,
7+
type ShallowRef,
78
type DebuggerEvent,
89
TrackOpTypes,
910
TriggerOpTypes,

0 commit comments

Comments
 (0)