I have a lot of data that I store in arrays. All array items have a string unique ID (like 123e4567-e89b-12d3-a456-426614174000). Before pushing an item, I call findIndex to make sure no duplicates are inserted with the same ID. I frequently access array items by index and by ID (find(i => i.id === UUID))
Is there a more efficient data structure for this? Supporting these features:
- ordered items (should support
push,unshift) - get item by ID in O(1) time (faster
find) - get item index by ID in O(1) time (faster
findIndex) - get items by index (
arr[index]) - no duplication by ID (#2 will make this easy to support)
- easy iteration (
for..of/forEach) - sorting items
I looked into a regular object and a Map but they don't support accessing items by index or sorting.