Skip to content

Commit

Permalink
Compute FDBIndex.count and FDBObjectStore.count directly rather than …
Browse files Browse the repository at this point in the history
…using a cursor, should improve performance
  • Loading branch information
dumbmatter committed Dec 29, 2023
1 parent 4c01ec3 commit 9230cfb
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 17 deletions.
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);
};
};
});
});

0 comments on commit 9230cfb

Please sign in to comment.