Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
divdavem committed Nov 8, 2024
1 parent 83920d9 commit 4849e89
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 0 deletions.
32 changes: 32 additions & 0 deletions benchmarks/js-reactivity-benchmarks/kairo/avoidable.bench.ts
Original file line number Diff line number Diff line change
@@ -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);
}
});
35 changes: 35 additions & 0 deletions benchmarks/js-reactivity-benchmarks/kairo/broad.bench.ts
Original file line number Diff line number Diff line change
@@ -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<number> = 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);
});
35 changes: 35 additions & 0 deletions benchmarks/js-reactivity-benchmarks/kairo/deep.bench.ts
Original file line number Diff line number Diff line change
@@ -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<number>;
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);
});

0 comments on commit 4849e89

Please sign in to comment.