Back

How IndexedDB Compares to LocalStorage and SessionStorage

How IndexedDB Compares to LocalStorage and SessionStorage

When building modern web applications, choosing the right client-side storage API can significantly impact your app’s performance and user experience. While LocalStorage and SessionStorage work well for simple data, IndexedDB offers capabilities that make it essential for offline web apps and complex data management. Let’s examine how these JavaScript browser storage options compare and when to use each.

Key Takeaways

  • LocalStorage and SessionStorage are synchronous APIs limited to 5-10MB of string data
  • IndexedDB provides asynchronous, non-blocking operations with virtually unlimited storage
  • IndexedDB supports complex data types, transactions, and indexing for efficient queries
  • Choose storage based on data complexity: simple strings use LocalStorage, complex objects need IndexedDB

Understanding the Three Client-Side Storage APIs

LocalStorage: Simple and Persistent

LocalStorage stores key-value pairs as strings that persist indefinitely until explicitly cleared. With a typical capacity of 5-10MB per origin, it’s ideal for user preferences, theme settings, or small configuration data.

localStorage.setItem('userTheme', 'dark');
const theme = localStorage.getItem('userTheme'); // Returns 'dark'

The synchronous nature of LocalStorage means every operation blocks the main thread. Reading or writing just 1MB of data can freeze your UI for 100-200 milliseconds on average devices.

SessionStorage: Temporary Tab Storage

SessionStorage works identically to LocalStorage but with one crucial difference: data expires when the tab closes. It maintains the same 5-10MB limit and synchronous API, making it suitable for temporary form data or single-session state management.

sessionStorage.setItem('formDraft', JSON.stringify(formData));

Data remains isolated per tab—opening the same page in a new tab creates a separate SessionStorage instance.

IndexedDB vs LocalStorage and SessionStorage: Key Differences

Storage Capacity and Data Types

While LocalStorage and SessionStorage restrict you to strings and 5-10MB of space, IndexedDB handles virtually unlimited storage (often 50% of available disk space) and stores JavaScript objects directly:

// IndexedDB stores complex objects without serialization
const userData = {
  id: 1,
  profile: { name: 'Alice', avatar: blobData },
  settings: { theme: 'dark', notifications: true },
  lastSync: new Date()
};
// Note: Map objects cannot be directly stored in IndexedDB

Performance Characteristics

The most critical difference lies in performance. LocalStorage and SessionStorage use synchronous operations that block JavaScript execution:

  • LocalStorage/SessionStorage: Synchronous, blocks main thread
  • IndexedDB: Asynchronous, non-blocking operations

For larger datasets, LocalStorage can noticeably block the UI due to its synchronous nature, while IndexedDB operations run asynchronously and keep the UI responsive.

Data Integrity and Transactions

IndexedDB provides transactional operations ensuring data consistency. If any part of a transaction fails, all changes roll back automatically—crucial for offline web apps maintaining data integrity.

When IndexedDB Outperforms LocalStorage

Offline-First Applications

Consider a note-taking app that works offline. With IndexedDB, you can store thousands of documents with attachments, search them efficiently using indexes, and sync changes when reconnected—impossible with LocalStorage’s limitations.

Large Dataset Management

E-commerce sites can cache entire product catalogs in IndexedDB. Applications like Linear use this approach to store project data locally, checking cache validity before making server requests, significantly reducing load times.

Complex Data Structures

IndexedDB handles Blobs, Files, and typed arrays natively. A photo editing app can store images directly without base64 encoding (which increases size by 33% in LocalStorage).

Practical Implementation Comparison

Here’s a real-world example storing user data:

// LocalStorage - requires serialization, blocks UI
const saveToLocalStorage = (data) => {
  localStorage.setItem('userData', JSON.stringify(data)); // Blocks thread
};

// IndexedDB - handles objects, non-blocking (using idb library)
const saveToIndexedDB = async (data) => {
  const db = await openDB('MyApp', 1, {
    upgrade(db) {
      db.createObjectStore('users', { keyPath: 'id' });
    }
  });
  const tx = db.transaction('users', 'readwrite');
  await tx.objectStore('users').put(data); // Non-blocking
};

Browser Compatibility and Limitations

All three APIs enjoy universal browser support as of 2025. However, behavior varies in private browsing:

  • Safari: Enforces lower quotas in private mode compared to normal browsing
  • Firefox: Clears all storage when private session ends
  • Chrome: Maintains standard quotas in incognito

Storage eviction policies also differ. Browsers may clear storage when disk space runs low, with IndexedDB data marked as “persistent” receiving protection from automatic deletion.

Making the Right Choice

Choose LocalStorage for:

  • User preferences under 1MB
  • Simple string data
  • Quick prototypes

Choose SessionStorage for:

  • Form drafts
  • Temporary UI state
  • Single-tab workflows

Choose IndexedDB for:

  • Offline functionality
  • Data over 5MB
  • Complex objects and files
  • Search and filtering requirements
  • Transaction integrity needs

Conclusion

While LocalStorage and SessionStorage remain valuable for simple storage needs, IndexedDB is essential for building performant, offline-capable web applications. Its asynchronous nature, unlimited capacity, and transaction support make it the only viable choice for storing large datasets or complex data structures. For modern JavaScript browser storage needs beyond basic key-value pairs, IndexedDB is the recommended choice for complex or large-scale client-side storage.

FAQs

No, IndexedDB cannot store Map and Set objects directly. You need to convert them to regular objects or arrays before storing. Use Array.from() for Sets and Object.fromEntries() for Maps, then reconstruct them when retrieving the data.

Clearing browser cache typically doesn't affect IndexedDB data. Users must specifically clear site data or cookies to remove IndexedDB content. However, browsers may evict IndexedDB data under storage pressure unless marked as persistent using the Storage API.

For tiny data under 100KB, LocalStorage can be faster for simple reads due to its synchronous nature. However, IndexedDB's non-blocking operations prevent UI freezes, making it better for user experience even with small datasets in production applications.

Understand every bug

Uncover frustrations, understand bugs and fix slowdowns like never before with OpenReplay — the open-source session replay tool for developers. Self-host it in minutes, and have complete control over your customer data. Check our GitHub repo and join the thousands of developers in our community.

OpenReplay