IndexedDB > Web Storage
IndexedDB > Web Storage
By Dave Bitter
7 min read
Explore the advantages of IndexedDB over the Web Storage API as a more efficient and sophisticated alternative for storing structured data on the client, eliminating the need for repetitive JSON parsing and stringifying operations.
- Authors
- Name
- Dave Bitter
- linkedinDave Bitter
- twitter@dave_bitter
- Github
- githubDaveBitter
- Website
- websiteBlog
I often use LocalStorage (or SessionStorage) to store structured data on the client for demos and real projects. While this worked fine, I’ve always had a nagging feeling that there would be a better way than JSON.parse
and JSON.stringify
large arrays on every change. Let’s have a look at why IndexedDB might be a better choice for you than the Storage API.
The LocalStorage and SessionStorage are herded under the Web Storage API. I will compare the Web Storage API to the IndexedDB API in this article. While you’ll see that IndexedDB is far richer in features, I will try to stick to the basics to meet my needs in storing structured data persisted in the user’s browser.
Oranges and Apples
In essence, with the Storage API, you’re limited to a get and set method where you can pass a key and a (string) value:
window.localStorage.setItem(
'tasks',
JSON.stringify([
{
id: 1,
title: 'Learn JavaScript',
completed: true,
},
{
id: 2,
title: 'Learn Vue',
completed: false,
},
{
id: 3,
title: 'Build something awesome',
completed: false,
},
])
)
const tasks = JSON.parse(window.localStorage.getItem('tasks'))
This is great for simple key-value storing, but not so much for more structured data. A quick comparison:
Feature | Web Storage | IndexedDB |
---|---|---|
Data Structure | Key-value pairs | Object store, key-value pairs |
Storage Limit | Typically around 5MB per origin | Larger storage capacity (varies per browser) |
Querying | Limited querying capabilities | Advanced querying with indexes |
Transactional | No | Yes |
Indexing | No | Support for indexes |
Data Types | Limited to strings | Supports complex data structures |
Browser Support | Widely supported across browsers | Widely supported across browsers |
Data Retention | Persistent | Persistent |
Use Cases | Storing simple data, small amounts | Storing structured data, larger amounts |
More simply put, the storage API is a key-value store while IndexedDB is an actual database. So why do we try to jam everything in the key-value store? Well, it’s easy! The path of least resistance is often chosen while it might make the actual code for updating and deleting items more complex.
Using IndexedDB
Like every technique I like to learn, I first try to create an actual demo so I run into real-world requirements and issues while building. This time, I went for a good old todo app that persists data in the user’s browser:
You can view the demo here and view the source code over at GitHub.
As I’m going to add quite a bit of functionality for the IndexedDB database, I’ll first create a helper in a separate file structured like this:
let openRequest
export const db = {
// Open the database and create the tasks object store if needed
openDB: () => {
return new Promise((resolve, reject) => {})
},
// Add a task to the database
addTask: (task) => {
return new Promise((resolve, reject) => {})
},
// Get all tasks from the database
getAllTasks: () => {
return new Promise((resolve, reject) => {})
},
// Update a task in the database
updateTask: (task) => {
return new Promise((resolve, reject) => {})
},
// Delete a task from the database
deleteTask: (id) => {
return new Promise((resolve, reject) => {})
},
}
With these methods, I should be able build all the functionality the todo app. Let’s go over them one by one.
Opening the database store
With IndexedDB you need an object store for data. You can see this as a collection in your database. I need to either retrieve an existing tasks store or create a new one if it doesn’t exist yet. You can do this like this:
let openRequest
export const db = {
// Open the database and create the tasks object store if needed
openDB: () => {
return new Promise((resolve, reject) => {
openRequest = indexedDB.open('tasks', 1)
openRequest.onupgradeneeded = () => {
const db = openRequest.result
if (!db.objectStoreNames.contains('tasks')) {
db.createObjectStore('tasks', { keyPath: 'id', autoIncrement: true })
}
}
openRequest.onsuccess = () => {
resolve(openRequest.result)
}
openRequest.onerror = () => {
reject(openRequest.error)
}
})
},
// ...
}
First I create a variable called openRequest
to store the opened database to be used in other methods coming up next. Then I call indexedDB.open('tasks', 1)
and store it in the variable. The first parameter is the name of the store, the second one is the version of the database.
You can use onupgradeneeded
to create object stores. There I check wether it already exists or I need to create one. When creating, you notice that I pass a keyPath
. By specifying that the keyPath
is the id
property of the task, I can later easily update or delete a task by passing the id
of the task. By passing autoIncrement: true
IndexedDB will automatically fill this property incremented based on the highest id
. I can call onsuccess
and onerror
to handle whether the object store was created successfully or not.
Great! I now have an object store to work with! Let’s add the actual methods allowing me to perform CRUD operations.
Creating a new task
Firstly, I want to be able to add a new task to the object store. I’ve build a form to gather the data and now need a method to pass the task object to:
let openRequest
export const db = {
// ...
// Add a task to the database
addTask: (task) => {
return new Promise((resolve, reject) => {
const db = openRequest.result
const transaction = db.transaction(['tasks'], 'readwrite')
const store = transaction.objectStore('tasks')
const addRequest = store.add(task)
addRequest.onsuccess = () => {
resolve(addRequest.result)
}
addRequest.onerror = () => {
reject(addRequest.error)
}
})
},
// ...
}
First, I get the database from the openRequest
by getting the result
property. Next I need to create a transaction. A transaction is basically series of operations that all need to pass successfully in order for the entire transaction to be applied. If one operation fails, all the operations won’t be applied. In this article I won’t go to deep into this, but you can read more on transactions here. First I pass an array of object stores I want to modify. In my use case, I only need to make an update to 'tasks'
. Secondly I pass the type of operations I want to perform. As I’m retrieving and writing data I need to pass 'readwrite'
.
Next, I get the 'tasks'
store from the transaction and I call the add
method with the task data. Again, I have onsuccess
and onerror
handlers that I can use to provide feedback in my application.
And that’s it! Note how much easier this code is compared to using the WebStorage API and having to get all the tasks, go over all of them until I find the one to update, update the task and storing the entire list again. With IndexedDB I can just call the add
method.
Reading all tasks
The second step is to create a method to retrieve all the tasks for the object store so I can display them in the UI. Let’s create a method that does just that:
let openRequest
export const db = {
// ...
// Get all tasks from the database
getAllTasks: () => {
return new Promise((resolve, reject) => {
const db = openRequest.result
const transaction = db.transaction(['tasks'], 'readonly')
const store = transaction.objectStore('tasks')
const getAllRequest = store.getAll()
getAllRequest.onsuccess = () => {
resolve(getAllRequest.result)
}
getAllRequest.onerror = () => {
reject(getAllRequest.error)
}
})
},
// ...
}
This method works much like the addTask
method. The only real difference is that I just need readonly
for the transaction and I call the getAll
method to retrieve all tasks.
Updating a task
As you might have noticed in the Todo demo, I have a checkbox where I can set the status of a task to be complete or incomplete. For this, I need to be able to make an update to (the status
property of) the task:
let openRequest
export const db = {
// ...
// Update a task in the database
updateTask: (task) => {
return new Promise((resolve, reject) => {
const db = openRequest.result
const transaction = db.transaction(['tasks'], 'readwrite')
const store = transaction.objectStore('tasks')
const putRequest = store.put(task)
putRequest.onsuccess = () => {
resolve(putRequest.result)
}
putRequest.onerror = () => {
reject(putRequest.error)
}
})
},
// ...
}
The only real difference between this method and the addTask
method is that I call the put
method instead of add
.
Deleting a task
Finally, I want to be able to delete a task from the database:
let openRequest
export const db = {
// ...
// Delete a task from the database
deleteTask: (id) => {
return new Promise((resolve, reject) => {
const db = openRequest.result
const transaction = db.transaction(['tasks'], 'readwrite')
const store = transaction.objectStore('tasks')
const deleteRequest = store.delete(id)
deleteRequest.onsuccess = () => {
resolve(deleteRequest.result)
}
deleteRequest.onerror = () => {
reject(deleteRequest.error)
}
})
},
// ...
}
You guessed it, I just have to call delete
with the id of the task to remove a task.
Is IndexedDB worth moving over from the good ol’ simple Web Storage?
Although they both offers persistent storage on the user’s browser, I see a clear distinction between the two. Web Storage is great for storing simple key-value pairs. There is no boiler plating needed, you just get and set items. For structured data I feel IndexedDB offers a way better API. Yes, there is more API specific code needed, but let’s have a look at actually using the Web Storage API for the todo app. You are going to need to write the logic for updating, retrieving and deleting a specific item yourself. So although the IndexedDB API requires more API-specific code, you’ll end up with roughly the same code with either of the options.
By abstracting away the database logic into the helper I’ve shown before, you or your team still have simple methods to call in your application, but under water you are using a far more robust and structured-data specific solution to persist data on the user’s browser. Not to mention the other benefits like performance, larger storage capacity and advanced querying.
In the end, you have to decide for yourself. The goal of this article is not to make you feel bad about (perhaps mis-) using the Web Storage API, but rather show how easy it could be with an abstraction helper to use the IndexedDB API. From now on I’ll be using the IndexedDB to persist more complex data structures in the user’s browser for my applications. Thanks for learning together!
Upcoming events
Coven of Wisdom - Herentals - Winter `24 edition
Worstelen jij en je team met automated testing en performance? Kom naar onze meetup waar ervaren sprekers hun inzichten en ervaringen delen over het bouwen van robuuste en efficiënte applicaties. Schrijf je in voor een avond vol kennis, heerlijk eten en een mix van creativiteit en technologie! 🚀 18:00 – 🚪 Deuren open 18:15 – 🍕 Food & drinks 19:00 – 📢 Talk 1 20:00 – 🍹 Kleine pauze 20:15 – 📢 Talk 2 21:00 – 🙋♀️ Drinks 22:00 – 🍻 Tot de volgende keer? Tijdens deze meetup gaan we dieper in op automated testing en performance. Onze sprekers delen heel wat praktische inzichten en ervaringen. Ze vertellen je hoe je effectieve geautomatiseerde tests kunt schrijven en onderhouden, en hoe je de prestaties van je applicatie kunt optimaliseren. Houd onze updates in de gaten voor meer informatie over de sprekers en hun specifieke onderwerpen. Over iO Wij zijn iO: een groeiend team van experts die end-to-end-diensten aanbieden voor communicatie en digitale transformatie. We denken groot en werken lokaal. Aan strategie, creatie, content, marketing en technologie. In nauwe samenwerking met onze klanten om hun merken te versterken, hun digitale systemen te verbeteren en hun toekomstbestendige groei veilig te stellen. We helpen klanten niet alleen hun zakelijke doelen te bereiken. Samen verkennen en benutten we de eindeloze mogelijkheden die markten in constante verandering bieden. De springplank voor die visie is talent. Onze campus is onze broedplaats voor innovatie, die een omgeving creëert die talent de ruimte en stimulans geeft die het nodig heeft om te ontkiemen, te ontwikkelen en te floreren. Want werken aan de infinite opportunities van morgen, dat doen we vandaag.
| Coven of Wisdom Herentals
Go to page for Coven of Wisdom - Herentals - Winter `24 editionMastering Event-Driven Design
PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED Are you and your team struggling with event-driven microservices? Join us for a meetup with Mehmet Akif Tütüncü, a senior software engineer, who has given multiple great talks so far and Allard Buijze founder of CTO and founder of AxonIQ, who built the fundaments of the Axon Framework. RSVP for an evening of learning, delicious food, and the fusion of creativity and tech! 🚀 18:00 – 🚪 Doors open to the public 18:15 – 🍕 Let’s eat 19:00 – 📢 Getting Your Axe On Event Sourcing with Axon Framework 20:00 – 🍹 Small break 20:15 – 📢 Event-Driven Microservices - Beyond the Fairy Tale 21:00 – 🙋♀️ drinks 22:00 – 🍻 See you next time? Details: Getting Your Axe On - Event Sourcing with Axon Framework In this presentation, we will explore the basics of event-driven architecture using Axon Framework. We'll start by explaining key concepts such as Event Sourcing and Command Query Responsibility Segregation (CQRS), and how they can improve the scalability and maintainability of modern applications. You will learn what Axon Framework is, how it simplifies implementing these patterns, and see hands-on examples of setting up a project with Axon Framework and Spring Boot. Whether you are new to these concepts or looking to understand them more, this session will provide practical insights and tools to help you build resilient and efficient applications. Event-Driven Microservices - Beyond the Fairy Tale Our applications need to be faster, better, bigger, smarter, and more enjoyable to meet our demanding end-users needs. In recent years, the way we build, run, and operate our software has changed significantly. We use scalable platforms to deploy and manage our applications. Instead of big monolithic deployment applications, we now deploy small, functionally consistent components as microservices. Problem. Solved. Right? Unfortunately, for most of us, microservices, and especially their event-driven variants, do not deliver on the beautiful, fairy-tale-like promises that surround them.In this session, Allard will share a different take on microservices. We will see that not much has changed in how we build software, which is why so many “microservices projects” fail nowadays. What lessons can we learn from concepts like DDD, CQRS, and Event Sourcing to help manage the complexity of our systems? He will also show how message-driven communication allows us to focus on finding the boundaries of functionally cohesive components, which we can evolve into microservices should the need arise.
| Coven of Wisdom - Utrecht
Go to page for Mastering Event-Driven DesignThe Leadership Meetup
PLEASE RSVP SO THAT WE KNOW HOW MUCH FOOD WE WILL NEED What distinguishes a software developer from a software team lead? As a team leader, you are responsible for people, their performance, and motivation. Your output is the output of your team. Whether you are a front-end or back-end developer, or any other discipline that wants to grow into the role of a tech lead, RSVP for an evening of learning, delicious food, and the fusion of leadership and tech! 🚀 18:00 – 🚪 Doors open to the public 18:15 – 🍕 Let’s eat 19:00 – 📢 First round of Talks 19:45 – 🍹 Small break 20:00 – 📢 Second round of Talks 20:45 – 🙋♀️ drinks 21:00 – 🍻 See you next time? First Round of Talks: Pixel Perfect and Perfectly Insane: About That Time My Brain Just Switched Off Remy Parzinski, Design System Lead at Logius Learn from Remy how you can care for yourself because we all need to. Second Round of Talks: Becoming a LeadDev at your client; How to Fail at Large (or How to Do Slightly Better) Arno Koehler Engineering Manager @ iO What are the things that will help you become a lead engineer? Building Team Culture (Tales of trust and positivity) Michel Blankenstein Engineering Manager @ iO & Head of Technology @ Zorggenoot How do you create a culture at your company or team? RSVP now to secure your spot, and let's explore the fascinating world of design systems together!
| Coven of Wisdom - Amsterdam
Go to page for The Leadership Meetup