-
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.
- Loading branch information
Showing
7 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
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
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
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 |
---|---|---|
|
@@ -3,3 +3,4 @@ dist | |
temp | ||
coverage | ||
.angular | ||
benchmarks.json |
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,82 @@ | ||
import { bench, describe } from 'vitest'; | ||
import type { ReadableSignal, Unsubscriber } from '../src'; | ||
import { computed, derived, DerivedStore, readable, Store, writable } from '../src'; | ||
|
||
class StoreClass extends Store<number> {} | ||
class DoubleStoreClass extends DerivedStore<number, ReadableSignal<number>> { | ||
protected override derive(value: number): Unsubscriber | void { | ||
this.set(2 * value); | ||
} | ||
} | ||
|
||
describe('creating base stores', () => { | ||
bench('writable', () => { | ||
writable(0); | ||
}); | ||
bench('readable', () => { | ||
readable(0); | ||
}); | ||
|
||
bench('new StoreClass', () => { | ||
new StoreClass(0); | ||
}); | ||
}); | ||
|
||
describe('creating derived stores', () => { | ||
const baseStore = writable(0); | ||
bench('computed', () => { | ||
computed(() => 2 * baseStore()); | ||
}); | ||
|
||
bench('derived', () => { | ||
derived(baseStore, (a) => 2 * a); | ||
}); | ||
|
||
bench('new DoubleStoreClass', () => { | ||
new DoubleStoreClass(baseStore, 0); | ||
}); | ||
}); | ||
|
||
describe('updating derived stores', () => { | ||
const baseStore1 = writable(0); | ||
computed(() => 2 * baseStore1()).subscribe(() => {}); | ||
let count1 = 0; | ||
bench('computed', () => { | ||
count1++; | ||
baseStore1.set(count1); | ||
}); | ||
|
||
const baseStore2 = writable(0); | ||
derived(baseStore2, (a) => 2 * a).subscribe(() => {}); | ||
let count2 = 0; | ||
bench('derived', () => { | ||
count2++; | ||
baseStore2.set(count2); | ||
}); | ||
|
||
const baseStore3 = writable(0); | ||
new DoubleStoreClass(baseStore3, 0).subscribe(() => {}); | ||
let count3 = 0; | ||
bench('DoubleStoreClass', () => { | ||
count3++; | ||
baseStore3.set(count3); | ||
}); | ||
}); | ||
|
||
describe('updating writable stores', () => { | ||
const storeWithoutSubscriber = writable(0); | ||
let count1 = 0; | ||
|
||
bench('without subscriber', () => { | ||
count1++; | ||
storeWithoutSubscriber.set(count1); | ||
}); | ||
|
||
const storeWithSubscriber = writable(0); | ||
storeWithSubscriber.subscribe(() => {}); | ||
let count2 = 0; | ||
bench('with subscriber', () => { | ||
count2++; | ||
storeWithSubscriber.set(count2); | ||
}); | ||
}); |
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,38 @@ | ||
import { RunnerTestFile } from 'vitest'; | ||
import { Reporter } from 'vitest/reporters'; | ||
import { writeFile } from 'fs/promises'; | ||
|
||
class JsonArrayReporter implements Reporter { | ||
async onFinished(files: RunnerTestFile[]): Promise<void> { | ||
const results: { | ||
name: string; | ||
unit: string; | ||
value: number; | ||
}[] = []; | ||
|
||
function processTasks(tasks: RunnerTestFile['tasks'], name: string) { | ||
for (const task of tasks) { | ||
if (task.type === 'suite') { | ||
processTasks(task.tasks, `${name} > ${task.name}`); | ||
} else { | ||
const value = task.result?.benchmark?.hz; | ||
if (value) { | ||
results.push({ | ||
name: `${name} > ${task.name}`, | ||
unit: 'Hz', | ||
value, | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
for (const file of files) { | ||
processTasks(file.tasks, file.name); | ||
} | ||
|
||
await writeFile('benchmarks.json', JSON.stringify(results, null, ' ')); | ||
} | ||
} | ||
|
||
export default JsonArrayReporter; |
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
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 |
---|---|---|
@@ -1,13 +1,19 @@ | ||
import { defineConfig } from 'vitest/config'; | ||
import JsonArrayReporter from './benchmarks/jsonArrayReporter'; | ||
|
||
export default defineConfig({ | ||
test: { | ||
setupFiles: ['test.ts'], | ||
include: ['./**/*.spec.ts'], | ||
include: ['src/**/*.spec.ts'], | ||
environment: 'happy-dom', | ||
coverage: { | ||
provider: 'v8', | ||
reporter: ['lcov'], | ||
include: ['src/**'], | ||
}, | ||
benchmark: { | ||
include: ['benchmarks/**/*.bench.ts'], | ||
reporters: [new JsonArrayReporter(), 'default'], | ||
}, | ||
}, | ||
}); |