dumbmatter / fakeIndexedDB

A pure JS in-memory implementation of the IndexedDB API
Apache License 2.0
562 stars 69 forks source link

`IDBObjectStore.count` is `O(n^2)` #94

Closed richvdh closed 8 months ago

richvdh commented 8 months ago

The implementation of IDBObjectStore.count uses a cursor internally, which would be fine, except that iterating a cursor is O(n) in the number of entries in the store (FDBCursor.iterate looks through all entries until it finds one whose position is greater than that of the cursor).

The net result is that IDBObjectStore.count is extremely slow, to the extent that it's much faster to call IDBObjectStore.getAllKeys() and count the results in the array... provided, of course, that it fits in memory.

dumbmatter commented 8 months ago

Yeah that is excessively slow! Well, a lot of things in this library are not exactly optimized for speed, but if it's a small amount of code for a big speed up, then that should be done.

Does #95 fix things for your use case?

richvdh commented 8 months ago

Wow, that was quick! Yes, that's much better, thank you!

dumbmatter commented 8 months ago

I saw the notification and was like "come on, no way it's that slow" so I had to investigate and... it was that slow lol

Released in version 5.0.2