From 4849e89c84b451ed6ab9cbd6e66613ab353affe0 Mon Sep 17 00:00:00 2001 From: David-Emmanuel DIVERNOIS Date: Fri, 8 Nov 2024 18:43:23 +0100 Subject: [PATCH] Adding benchmarks from https://github.com/milomg/js-reactivity-benchmark --- .../kairo/avoidable.bench.ts | 32 +++++++++++++++++ .../kairo/broad.bench.ts | 35 +++++++++++++++++++ .../kairo/deep.bench.ts | 35 +++++++++++++++++++ 3 files changed, 102 insertions(+) create mode 100644 benchmarks/js-reactivity-benchmarks/kairo/avoidable.bench.ts create mode 100644 benchmarks/js-reactivity-benchmarks/kairo/broad.bench.ts create mode 100644 benchmarks/js-reactivity-benchmarks/kairo/deep.bench.ts diff --git a/benchmarks/js-reactivity-benchmarks/kairo/avoidable.bench.ts b/benchmarks/js-reactivity-benchmarks/kairo/avoidable.bench.ts new file mode 100644 index 0000000..595c6c7 --- /dev/null +++ b/benchmarks/js-reactivity-benchmarks/kairo/avoidable.bench.ts @@ -0,0 +1,32 @@ +// adapted from https://github.com/milomg/js-reactivity-benchmark/blob/main/src/kairo/avoidable.ts + +import { bench } from 'vitest'; +import { computed, writable } from '../../../src'; + +function busy() { + let a = 0; + for (let i = 0; i < 1_00; i++) { + // eslint-disable-next-line @typescript-eslint/no-unused-vars + a++; + } +} + +const head = writable(0); +const computed1 = computed(() => head()); +const computed2 = computed(() => (computed1(), 0)); +const computed3 = computed(() => (busy(), computed2() + 1)); // heavy computation +const computed4 = computed(() => computed3() + 2); +const computed5 = computed(() => computed4() + 3); +computed(() => { + computed5(); + busy(); // heavy side effect +}).subscribe(() => {}); + +bench('avoidablePropagation', () => { + head.set(1); + console.assert(computed5() === 6); + for (let i = 0; i < 10; i++) { + head.set(i); + console.assert(computed5() === 6); + } +}); diff --git a/benchmarks/js-reactivity-benchmarks/kairo/broad.bench.ts b/benchmarks/js-reactivity-benchmarks/kairo/broad.bench.ts new file mode 100644 index 0000000..4d8b876 --- /dev/null +++ b/benchmarks/js-reactivity-benchmarks/kairo/broad.bench.ts @@ -0,0 +1,35 @@ +// adapted from https://github.com/milomg/js-reactivity-benchmark/blob/main/src/kairo/broad.ts + +import { bench } from 'vitest'; +import { computed, writable } from '../../../src'; +import type { ReadableSignal } from '../../../src'; + +const loopCount = 10; + +const head = writable(0); +let last: ReadableSignal = head; +let callCounter = 0; +for (let i = 0; i < loopCount; i++) { + const current = computed(() => { + return head() + i; + }); + const current2 = computed(() => { + return current() + 1; + }); + computed(() => { + current2(); + callCounter++; + }).subscribe(() => {}); + last = current2; +} + +bench('broad', () => { + head.set(1); + const atleast = loopCount * loopCount; + callCounter = 0; + for (let i = 0; i < loopCount; i++) { + head.set(i); + console.assert(last() === i + loopCount); + } + console.assert(callCounter === atleast, callCounter); +}); diff --git a/benchmarks/js-reactivity-benchmarks/kairo/deep.bench.ts b/benchmarks/js-reactivity-benchmarks/kairo/deep.bench.ts new file mode 100644 index 0000000..3b71774 --- /dev/null +++ b/benchmarks/js-reactivity-benchmarks/kairo/deep.bench.ts @@ -0,0 +1,35 @@ +// adapted from https://github.com/milomg/js-reactivity-benchmark/blob/main/src/kairo/deep.ts + +import { bench } from 'vitest'; +import { writable, computed } from '../../../src'; +import type { ReadableSignal } from '../../../src'; + +const len = 50; + +const head = writable(0); +let current = head as ReadableSignal; +for (let i = 0; i < len; i++) { + const c = current; + current = computed(() => { + return c() + 1; + }); +} +let callCounter = 0; + +computed(() => { + current(); + callCounter++; +}).subscribe(() => {}); + +const iter = 50; + +bench('deep', () => { + head.set(1); + const atleast = iter; + callCounter = 0; + for (let i = 0; i < iter; i++) { + head.set(i); + console.assert(current() === len + i); + } + console.assert(callCounter === atleast); +});