Skip to content

Commit

Permalink
performance tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lifeart committed May 16, 2024
1 parent c01bdd3 commit 728e698
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/wrapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,44 @@ describe("currentComputed", () => {
});
});

describe('Performance', () => {
it('per-item unwatch is fast', () => {
const amountOfSignals = 10000;
const unwatchToWatchSlownessRatio = 15;
const w = new Signal.subtle.Watcher(() => {});
const signals = Array.from({length: amountOfSignals}, () => new Signal.State(1));
const watchStart = performance.now();
signals.forEach(s => w.watch(s));
const watchEnd = performance.now();
const unwatchStart = performance.now();
signals.forEach(s => w.unwatch(s));
const unwatchEnd = performance.now();
const watchTime = watchEnd - watchStart;
const unwatchTime = unwatchEnd - unwatchStart;

// we assume that unwatch can't be N times slower than watch
expect(unwatchTime).toBeLessThan(watchTime * unwatchToWatchSlownessRatio);

});
it('batch unwatch is fast', () => {
const amountOfSignals = 10000;
const unwatchToWatchSlownessRatio = 2;
const w = new Signal.subtle.Watcher(() => {});
const signals = Array.from({length: amountOfSignals}, () => new Signal.State(1));
const watchStart = performance.now();
signals.forEach(s => w.watch(s));
const watchEnd = performance.now();
const unwatchStart = performance.now();
w.unwatch(...signals);
const unwatchEnd = performance.now();
const watchTime = watchEnd - watchStart;
const unwatchTime = unwatchEnd - unwatchStart;

expect(unwatchTime).toBeLessThan(watchTime * unwatchToWatchSlownessRatio);

});
})

// Some things which we're comfortable with not hitting in code coverage:
// - The code for the callbacks (for reading signals and running watches)
// - Paths around writes being prohibited during computed/effect
Expand Down

0 comments on commit 728e698

Please sign in to comment.