AY

Browser Storage Methods: Storage API vs IndexedDB vs Cookies

JavaScript

Alex | Last updated: April 15, 2024

Modern web browsers provide three main ways of client storage:

  1. Web Storage Api (Local/Session Storage)
  2. IndexedDB
  3. Cookies

Web Storage API

The conventional client storage solution has long been the Web Storage API. There are two mechanisms for Web Storage:

The Storage API is useful for storing non-sensitive data for use within client scripts, such as user settings in localStoragesessionStorage can be used to temporarily store usernames and passwords, though some might advise against this. Data is stored as key-value pairs, which makes primitive data easy to read and write.

While the base API is easy to work with, the Web Storage API only accepts strings as values; to store anything more complicated, such as a JSON object, one must stringify the object before storing it and parse the object when retrieving it. This also means the developer must manually encode a versioning schema and write custom migration logic for anything more complicated than primitive strings to prevent data corruption issues. It’s a pain.

WebStorage generally has a storage limit of 5mbs. Moreover, Web Storage is synchronous in nature. Writing and reading from Web Storage will block the main thread and prevent other JavaScript code until complete. Web Storage is also not available for use from within Web Workers. There are also potential security issues regarding CORS and a client’s ability to trivially tamper with stored values. Sad.

In modern web development, the Web Storage API has largely been replaced by IndexedDB; if you want to store anything more complicated than key-value pairs, use IndexedDB.

IndexedDB

Why IndexedDB is Great

IndexedDB is a great client storage solution, and it is my preferred method of storing things locally—sans auth. IndexedDB

IndexedDB’s Shortcomings

In my experience working with IndexedDB so far, there aren’t too many shortcomings other than:

Ways to Work with IndexedDB

If you want to avoid using the out-of-the-box native IndexedDB API, there are two popular options:

  1. idb (GitHub): “IndexedDB with usability.” I prefer this one, and there is a pretty helpful guide to working with the IndexedDB Promised library.
  2. Dexie (npm, GitHub): Wrapper with more robust features, such as Sync.

Cookies

Browser cookies are meant to be small pieces of data a server sends to a user’s web browser to be used for later server requests. They are most commonly used for user authentication/access persistence and tracking.

Cookies can be made relatively safe against Cross-Site Scripting (XSS) by setting the HTTP only flag to true—this prevents JavaScript from accessing cookies, which is fine as cookies are mainly used to preserve authentication for network requests and read server-side—and imposing an expiration date. However, without SSL encryption, they are susceptible to being intercepted on open networks. As with the Storage API, cookies are also susceptible to being tampered with by the client.

Of all the browser storage solutions, cookies are the most restricted in terms of storage space:

All cookies valid for a given page on a given domain are sent from the browser to the server for every request to the same domain.

Summary

Web Storage APIIndexedDBCookies
Storage ValuesKey-value pairs restricted to strings only.Allows for storage, advanced querying, and indexing of complex objects.Key-value pairs restricted to strings only, alongside attributes that control when and where the cookie is used.
Storage Limits5MB storage limit.Storage limit determined by client storage availability.300 max cookies for the browser—20 per domain, and 4096 bytes per cookie.
OperationsOperations are synchrounous, single-threaded, and blocking.Operations are asynchrounous.Operations are synchrounous, single-threaded, and blocking.
Complex ObjectsPossible to store complex objects using JSON methods, but versioning and migration logic must be custom and often complex.Versioning and migration largely handled out of the box.Complex object storage not practical, given storage limitations.
Client TamperingDifficult to prevent trivial tampering by user.Read-only from browser, sans delete operations.Difficult to prevent trivial tampering by user.
Security VulnerabilitiesVulnerable to XSS.Vulnerable to XSS.Vulnerable to data interception.