Skip to content

Commit 604c42d

Browse files
committed
chore(runtime-vapor): add benchmark build flag
1 parent eda2a43 commit 604c42d

File tree

9 files changed

+48
-5
lines changed

9 files changed

+48
-5
lines changed

benchmark/client/App.vue

+23-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<script setup lang="ts" vapor>
22
import {
3-
ref,
43
shallowRef,
54
triggerRef,
65
type ShallowRef,
@@ -11,7 +10,7 @@ import { defer, wrap } from './profiling'
1110
1211
const isVapor = !!import.meta.env.IS_VAPOR
1312
14-
const selected = ref<number>()
13+
const selected = shallowRef<number>()
1514
const rows = shallowRef<
1615
{
1716
id: number
@@ -79,10 +78,32 @@ async function bench() {
7978
}
8079
8180
const isSelected = createSelector(selected)
81+
82+
const globalThis = window
8283
</script>
8384

8485
<template>
8586
<h1>Vue.js ({{ isVapor ? 'Vapor' : 'Virtual DOM' }}) Benchmark</h1>
87+
88+
<div style="display: flex; gap: 4px; margin-bottom: 4px">
89+
<label>
90+
<input
91+
type="checkbox"
92+
:value="globalThis.doProfile"
93+
@change="globalThis.doProfile = $event.target.checked"
94+
/>
95+
Profiling
96+
</label>
97+
<label>
98+
<input
99+
type="checkbox"
100+
:value="globalThis.reactivity"
101+
@change="globalThis.reactivity = $event.target.checked"
102+
/>
103+
Reactivity Cost
104+
</label>
105+
</div>
106+
86107
<div
87108
id="control"
88109
style="display: flex; flex-direction: column; width: fit-content; gap: 6px"

benchmark/client/profiling.ts

+15-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22
/* eslint-disable no-restricted-syntax */
33
/* eslint-disable no-restricted-globals */
44

5-
declare module globalThis {
5+
declare namespace globalThis {
66
let doProfile: boolean
7+
let reactivity: boolean
78
let recordTime: boolean
89
let times: Record<string, number[]>
910
}
1011

1112
globalThis.recordTime = true
1213
globalThis.doProfile = false
14+
globalThis.reactivity = false
1315

1416
export const defer = () => new Promise(r => requestIdleCallback(r))
1517

@@ -34,7 +36,18 @@ export function wrap(
3436
fn(...args)
3537

3638
await defer()
37-
const time = performance.now() - start
39+
let time: number
40+
if (globalThis.reactivity) {
41+
time = performance.measure(
42+
'flushJobs-measure',
43+
'flushJobs-start',
44+
'flushJobs-end',
45+
).duration
46+
performance.clearMarks()
47+
performance.clearMeasures()
48+
} else {
49+
time = performance.now() - start
50+
}
3851
const prevTimes = times[id] || (times[id] = [])
3952
prevTimes.push(time)
4053

benchmark/index.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,11 @@ if (!skipBench) {
8888
async function buildLib() {
8989
console.info(colors.blue('Building lib...'))
9090

91+
/** @type {import('node:child_process').SpawnOptions} */
9192
const options = {
9293
cwd: path.resolve(import.meta.dirname, '..'),
9394
stdio: 'inherit',
95+
env: { ...process.env, BENCHMARK: 'true' },
9496
}
9597
const buildOptions = devBuild ? '-df' : '-pf'
9698
const [{ ok }, { ok: ok2 }, { ok: ok3 }, { ok: ok4 }] = await Promise.all([
@@ -130,7 +132,7 @@ async function buildApp(isVapor) {
130132
colors.blue(`\nBuilding ${isVapor ? 'Vapor' : 'Virtual DOM'} app...\n`),
131133
)
132134

133-
process.env.NODE_ENV = 'production'
135+
if (!devBuild) process.env.NODE_ENV = 'production'
134136
const CompilerSFC = await import(
135137
'../packages/compiler-sfc/dist/compiler-sfc.cjs.js'
136138
)

packages/global.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ declare var __CJS__: boolean
99
declare var __SSR__: boolean
1010
declare var __VERSION__: string
1111
declare var __COMPAT__: boolean
12+
declare var __BENCHMARK__: boolean
1213

1314
// Feature flags
1415
declare var __FEATURE_OPTIONS_API__: boolean

packages/runtime-vapor/src/scheduler.ts

+2
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ export function flushPostFlushCbs(): void {
139139

140140
// TODO: dev mode and checkRecursiveUpdates
141141
function flushJobs() {
142+
if (__BENCHMARK__) performance.mark('flushJobs-start')
142143
isFlushPending = false
143144
isFlushing = true
144145

@@ -169,6 +170,7 @@ function flushJobs() {
169170
if (queue.length || pendingPostFlushCbs.length) {
170171
flushJobs()
171172
}
173+
if (__BENCHMARK__) performance.mark('flushJobs-end')
172174
}
173175
}
174176

playground/setup/dev.js

+1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export function DevPlugin({ browser = false } = {}) {
5151
__NODE_JS__: String(false),
5252
// need SSR-specific branches?
5353
__SSR__: String(false),
54+
__BENCHMARK__: 'false',
5455

5556
// 2.x compat build
5657
__COMPAT__: String(false),

rollup.config.js

+1
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ function createConfig(format, output, plugins = []) {
218218
__CJS__: String(isCJSBuild),
219219
// need SSR-specific branches?
220220
__SSR__: String(!isGlobalBuild),
221+
__BENCHMARK__: process.env.BENCHMARK || 'false',
221222

222223
// 2.x compat build
223224
__COMPAT__: String(isCompatBuild),

scripts/dev.js

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ for (const target of targets) {
151151
__ESM_BROWSER__: String(format.includes('esm-browser')),
152152
__CJS__: String(format === 'cjs'),
153153
__SSR__: String(format !== 'global'),
154+
__BENCHMARK__: process.env.BENCHMARK || 'false',
154155
__COMPAT__: String(target === 'vue-compat'),
155156
__FEATURE_SUSPENSE__: `true`,
156157
__FEATURE_OPTIONS_API__: `true`,

vitest.config.ts

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default defineConfig({
1212
__ESM_BROWSER__: false,
1313
__CJS__: true,
1414
__SSR__: true,
15+
__BENCHMARK__: false,
1516
__FEATURE_OPTIONS_API__: true,
1617
__FEATURE_SUSPENSE__: true,
1718
__FEATURE_PROD_DEVTOOLS__: false,

0 commit comments

Comments
 (0)