scottohara / tvmanager

PWA for tracking recorded, watched & upcoming TV shows
MIT License
3 stars 0 forks source link

IndexedDB currently doesn't support case-insensitive sorting #94

Open scottohara opened 4 years ago

scottohara commented 4 years ago

Imagine a store that contains objects like this, where id is the primary key:

{ id, name }

You want to fetch ALL objects in the store, sorted by name.

The natural solution would be to index the name field, e.g.

store.createIndex("name", "name");

..then use that index to fetch the all records:

const records = await store.index("name").getAll();

However, if you want the objects sorted by name case-insensitively, you need to sort the results yourself in JS:

const temp = await store.index("name").getAll();
const records = temp.sort((a, b) => a.name.localeCompare(b.name, { sensitivity: "base" }));

Which then begs the question: for this particular use case, is there any benefit in having the name field indexed at all? (assuming that the only reason for adding the index was for ordering the results)

Or, given that you need to sort the results manually anyway, is it better to just get all object from the store in their default order:

const temp = (await store.getAll())
const records = temp.sort((a, b) => a.name.localeCompare(b.name, { sensitivity: "base" }));

On the one hand, you could argue that using the index will return results pre-sorted by name (case sensitively), and there may be less shuffling for JS to do to further put them in case-insensitive order? (my own rough benchmarking on ~125 records doesn’t bear this out).

On the other hand, the index on the name field (presumably) increases storage consumption.

There is a propose for locale-aware sorting (possibly through a new "collation" property on indexes): https://github.com/w3c/IndexedDB/issues/38