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

Improve FDBIndex.count and FDBObjectStore.count performance #95

Merged
merged 1 commit into from
Dec 30, 2023
Merged
Show file tree
Hide file tree
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
9 changes: 1 addition & 8 deletions src/FDBIndex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,14 +247,7 @@ class FDBIndex {

return this.objectStore.transaction._execRequestAsync({
operation: () => {
let count = 0;

const cursor = new FDBCursor(this, key);
while (cursor._iterate() !== null) {
count += 1;
}

return count;
return this._rawIndex.count(key);
},
source: this,
});
Expand Down
9 changes: 1 addition & 8 deletions src/FDBObjectStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -549,14 +549,7 @@ class FDBObjectStore {

return this.transaction._execRequestAsync({
operation: () => {
let count = 0;

const cursor = new FDBCursor(this, key);
while (cursor._iterate() !== null) {
count += 1;
}

return count;
return this._rawObjectStore.count(key);
},
source: this,
});
Expand Down
13 changes: 12 additions & 1 deletion src/lib/Index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Index {
name: string,
keyPath: KeyPath,
multiEntry: boolean,
unique: boolean
unique: boolean,
) {
this.rawObjectStore = rawObjectStore;

Expand Down Expand Up @@ -176,6 +176,17 @@ class Index {
source: null,
});
}

public count(range: FDBKeyRange) {
let count = 0;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const record of this.records.values(range)) {
count += 1;
}

return count;
}
}

export default Index;
11 changes: 11 additions & 0 deletions src/lib/ObjectStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ class ObjectStore {
rawIndex.records.clear();
}
}

public count(range: FDBKeyRange) {
let count = 0;

// eslint-disable-next-line @typescript-eslint/no-unused-vars
for (const record of this.records.values(range)) {
count += 1;
}

return count;
}
}

export default ObjectStore;
29 changes: 29 additions & 0 deletions src/test/fakeIndexedDB/fakeIndexedDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -831,4 +831,33 @@ describe("fakeIndexedDB Tests", () => {
const dataView2 = new DataView(key);
assert.equal(dataView2.getUint32(0), 1234567890);
});

it("FDBObjectStore.count performance should be reasonable (issue #94)", (done) => {
const N = 10000;
const openreq = fakeIndexedDB.open("test94");
openreq.onupgradeneeded = (event) => {
const db = event.target.result;
const store = db.createObjectStore("items", {
keyPath: "key",
autoIncrement: true,
});
for (let i = 0; i < N; i++) {
store.put({ i });
}
};
openreq.onsuccess = (event) => {
const db = event.target.result;
const request = db
.transaction("items")
.objectStore("items")
.count();
request.onsuccess = (event2: any) => {
assert.equal(event2.target.result, N);
done();
};
request.onerror = (event2: any) => {
done(event2.target.error);
};
};
});
});