示例工程 25-vue-reactivity-streaming

25-vue-reactivity-streaming/app/.tsc/src/lib/benchmark.js

javascript · 1.5 KB

import { reactive, shallowRef } from "vue";
const SOURCES = ["build", "network", "tools"];
export function makeBenchBatch(start, size) {
    return Array.from({ length: size }, (_, index) => {
        const id = start + index;
        return {
            id,
            ts: 1_700_000_000_000 + id,
            source: SOURCES[id % SOURCES.length] ?? "build",
            level: id % 17 === 0 ? "warn" : "info",
            text: `bench event ${id}`,
        };
    });
}
export function runBenchmark(rounds = 120, size = 60) {
    const deep = reactive([]);
    const deepStart = performance.now();
    for (let round = 0; round < rounds; round += 1) {
        deep.push(...makeBenchBatch(round * size, size));
    }
    const deepSum = deep.reduce((sum, event) => sum + event.id, 0);
    const deepMs = performance.now() - deepStart;
    let raw = [];
    const shallow = shallowRef(raw);
    const shallowStart = performance.now();
    for (let round = 0; round < rounds; round += 1) {
        raw = raw.concat(makeBenchBatch(round * size, size));
        shallow.value = raw;
    }
    const shallowSum = shallow.value.reduce((sum, event) => sum + event.id, 0);
    const shallowMs = performance.now() - shallowStart;
    return {
        rounds,
        size,
        deepMs: Math.round(deepMs * 100) / 100,
        shallowMs: Math.round(shallowMs * 100) / 100,
        deepSum,
        shallowSum,
        speedup: shallowMs > 0 ? Math.round((deepMs / shallowMs) * 10) / 10 : 0,
    };
}
//# sourceMappingURL=benchmark.js.map