-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding benchmarks from https://github.com/milomg/js-reactivity-benchmark
- Loading branch information
Showing
3 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
benchmarks/js-reactivity-benchmarks/kairo/avoidable.bench.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); |