IndexedDB Indexes: Fast Queries in the Browser

An IndexedDB index is like a book's index, letting you quickly find records by a specific property without scanning the entire dataset. It's essential for fast queries on non-primary keys, like looking up a user by email.
Why it exists
IndexedDB object stores let you retrieve objects by their primary key very quickly. But to find all objects where a different property has a certain value, like finding all users with is_admin: true, the browser would have to load and inspect every single object. This is incredibly slow for large datasets. Indexes solve this performance bottleneck.
The mental model
An IndexedDB index is a separate, sorted data structure that maps values of a specific property (the keyPath) back to the records in your main object store. It's like the index in the back of a textbook. Instead of reading the whole book to find mentions of a topic, you look it up in the index and get a list of page numbers, letting you jump directly to the data you need.
How it works
You create an index on an object store using the createIndex() method. This must be done inside an onupgradeneeded callback, which runs when the database version changes. You provide a name for the index and a keyPath specifying which property to index (e.g., 'email'). You can also set options. The unique: true option enforces that no two records can have the same value for the indexed property. The multiEntry: true option is powerful for array values (like a tags property); it creates an index entry for each item in the array, allowing you to efficiently query for records containing a specific tag.
When to use it
Use an index on any property you plan to query or sort by frequently. This is essential for filtering data, such as finding all products in a certain category, all users in a specific city, or all tasks marked as 'urgent'. It's the key to building responsive, data-heavy applications that don't freeze the UI during lookups.
When not to use it
Avoid creating indexes on properties you will never search by. Every index adds overhead: it consumes storage space and slightly slows down write operations (adds, updates, deletes) because the index must also be updated. Don't index everything "just in case." Also, be mindful of indexing large, complex objects or binary data, as this can be inefficient.
One canonical example
To find users by their email address, you would create an index on the 'email' property. During a database version upgrade, you'd call const userStore = db.createObjectStore('users', { keyPath: 'id' }); userStore.createIndex('emailIndex', 'email', { unique: true });. Later, to find a user, you can get the index db.transaction('users').objectStore('users').index('emailIndex') and use its get() method with an email address for a near-instant lookup, avoiding a full table scan.
Interview question
When deciding whether to create an IndexedDB index on a property, which factor is the MOST critical to consider?
- a.The size of the database, as indexes are only beneficial for very large datasets.
- b.The complexity of the property's data type, to avoid indexing large or binary objects.
- c.How often the application will perform search or sort operations using that specific property.Correct
- d.Whether the property's values are guaranteed to be unique across all records.
Why? this is the answer
The card explicitly states to "Use an index on any property you plan to query or sort by frequently" and to "Avoid creating indexes on properties you will never search by" due to overhead. While other factors like uniqueness or data complexity are mentioned, the frequency of query/sort operations is presented as the primary driver for the decision to create an index.
Just read this? Test yourself on what you have been reading.
Read the original → developer.mozilla.org
- #indexeddb
- #web apis
- #storage
- #performance
You just looked this up. Could you explain it out loud?
That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.
See open roles