IndexedDB — a browser-built-in NoSQL database storing structured data (records, blobs, files) on the client. Async API, transactional, supports indexes, 100+ MB storage. Used in PWAs for offline-first + large-volume cache. Replaces deprecated WebSQL. Alternatives: localForage (wrapper), Dexie.js (ORM-like).
Below: details, example, related terms, FAQ.
Free online tool — HTTP header checker: instant results, no signup.
const request = indexedDB.open('myDB', 1);
request.onupgradeneeded = e => {
e.target.result.createObjectStore('users', { keyPath: 'id' });
};
request.onsuccess = e => {
const tx = e.target.result.transaction('users', 'readwrite');
tx.objectStore('users').put({ id: 1, name: 'Alice' });
};IndexedDB provides a powerful API for storing and retrieving large amounts of structured data. Here are the basic operations to get you started:
indexedDB.open() method.const request = indexedDB.open('myDatabase', 1);In this example, 'myDatabase' is the name of your database, and '1' is the version number. You can also handle the onupgradeneeded event to create object stores:
request.onupgradeneeded = function(event) {
const db = event.target.result;
db.createObjectStore('myStore', { keyPath: 'id' });
};const transaction = db.transaction('myStore', 'readwrite');
const store = transaction.objectStore('myStore');
store.add({ id: 1, name: 'Item 1' });In this example, we're adding an object with an id and name to the 'myStore' object store.
get() method.const getRequest = store.get(1);
getRequest.onsuccess = function(event) {
console.log(event.target.result);
};This retrieves the object with id 1 from the store.
IndexedDB allows for complex queries and transactions, making it suitable for applications requiring a significant amount of client-side data management.
When it comes to client-side storage, IndexedDB is not the only option available. Here, we compare IndexedDB with other popular storage solutions:
In contrast, IndexedDB supports asynchronous operations, transactions, and large storage limits (over 100 MB). It is designed for applications requiring offline capabilities and the ability to handle complex data structures.
For developers, the choice between these options should be based on the specific needs of the application. IndexedDB is the best choice for applications that require robust data storage and retrieval capabilities, especially in Progressive Web Apps (PWAs).
When working with IndexedDB, following best practices can enhance performance and ensure a smoother user experience. Here are some key tips:
createIndex() method when setting up your object store.const store = db.transaction('myStore', 'readwrite').objectStore('myStore');
store.createIndex('nameIndex', 'name');With the above index, you can quickly search for records by name.
onerror event handler to catch and respond to errors appropriately.request.onerror = function(event) {
console.error('Database error: ' + event.target.errorCode);
};Clean Up Unused Data: Regularly review and remove outdated or unnecessary data from your database to keep it optimized.
By adhering to these best practices, developers can maximize the efficiency of their applications that utilize IndexedDB, ensuring they run smoothly and effectively even under heavy data loads.
localStorage — 5-10 MB, sync, string-only. IndexedDB — gigabytes, async, structured objects + indexes.
Yes, every modern browser (Safari iOS, Chrome Android). Safari is partially limited in PWA context.
When disk space is tight — yes. For critical data use <code>navigator.storage.persist()</code>.
Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.