Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf improvement: use array instead of set for queue #137

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@
const triggerUpdate = Symbol();
const queueProcess = Symbol();
let willProcessQueue = false;
const queue = new Set<{ [queueProcess](): void }>();
const queue: { [queueProcess](): void }[] = [];

const MAX_STORE_PROCESSING_IN_QUEUE = 1000;
const checkIterations = (iterations: number) => {
Expand Down Expand Up @@ -385,15 +385,17 @@
if (needsProcessQueue) {
try {
const storePasses = new Map<{ [queueProcess](): void }, number>();
for (const store of queue) {
while (queue.length > 0) {
const store = queue.shift()!;
const storeCount = storePasses.get(store) ?? 0;
checkIterations(storeCount);
storePasses.set(store, storeCount + 1);
queue.delete(store);
store[queueProcess]();
}
} finally {
queue.clear();
if (queue.length > 0) {
queue.splice(0, queue.length);

Check warning on line 397 in src/index.ts

View check run for this annotation

Codecov / codecov/patch

src/index.ts#L397

Added line #L397 was not covered by tests
}
willProcessQueue = false;
}
}
Expand Down Expand Up @@ -604,7 +606,10 @@
protected pauseSubscribers(): void {
if (!this.#subscribersPaused) {
this.#subscribersPaused = true;
queue.delete(this as any);
const indexInQueue = queue.indexOf(this as any);
if (indexInQueue > -1) {
queue.splice(indexInQueue, 1);
}
for (const subscriber of [...this.#subscribers]) {
if (subscriber._valueIndex === 0 || subscriber._paused) {
// ignore subscribers which were not yet called synchronously or are already paused
Expand All @@ -628,7 +633,7 @@
if (this.#subscribersPaused) {
this.#subscribersPaused = false;
batch(() => {
queue.add(this as any);
queue.push(this as any);
});
}
}
Expand Down