Skip to main content

How To Select a Browser Storage Option

We’re in a position that, for many projects, we must include some sort of browser storage. Unfortunately, we aren’t yet at the point where the best approach is entirely obvious. Between localStorage, WebSQL, and IndexedDB developers are left with a complicated pool of options and difficult to compare pros and cons.


localStorage

The simplest and widest supported option is localStorage. If you can solve your needs with it, you should. localStorage keeps simply key->value pairs of strings in the browser, even when your website is closed. There is no real structure to the information you store, making it good for simple cases like user preferences or caching of necessarily small things.

Use localStorage when
  • You need to store less than 2.5 MB, which varies a bit between browsers
  • You can deal with the storage failing, which can happen in private modes or when hitting storage limits. Some failures are silent
  • You can identify your stored data by easily known keys, and don’t need to query over it
  • You need support across all major browsers today

Don’t Use localStorage when
  • You need to store much larger amounts of information, or large binary files like images or music
  • You need to store some unknown amount of user created content, and search for it later
  • You need access to perform very well, with very frequent access

WebSQL

The WebSQL spec was introduced with a lot of excitement, because it was such a wonderfully simple solution. Not only was it SQL we already knew, but it was explicitly SQLite, one of the most well known and widely used SQL engines, when you need smaller and embedded databases in an application. WebSQL’s simple approach to transplant such a well known engine into our web browsers meant very little to learn to take advantage of this new API.
Unfortunately, the future of WebSQL is suspect. For many reasons, many of which crowds of developers disagree with, standards bodies will not support WebSQL. But, it is still implemented in a number of browsers, especially mobile browsers. Based on browser support, it is a great option, standard or not.

Do Use WebSQL when
  • You need support for pre-Android 4.4 mobile browsers or PhoneGap projects
  • You can live without Firefox support. Maybe you’re app is mobile/phonegap only or a Chrome App.
  • You’re porting something as directly as possible from an existing SQL-using application and schema. Maybe you’ll port this over to IndexedDB later.

Do Not Use WebSQL when
  • You care Firefox and IE support (I sure hope so)
  • You want to reduce long term support as WebSQL is deprecated and removed in the future
  • Your data is not well suited for relational storage

IndexedDB

Moving forward, while localStorage will continue to have its use in small and specific data sets, IndexedDB is king of the hill in the browser storage game. There isn’t much of a debate, because WebSQL is no longer supported and IndexedDB is implemented in recent versions of all major browsers, with the exception of both Desktop and Mobile Safari.

Compared to the key/value and table based storage provided by our other two options, IndexedDB “feels more webby” for better or worse. Serialized javascript objects are dumped directly into storage, rather than being constrained to key/value strings or a pre-defined table schema. “Here, keep a copy of this object, I’ll ask for it later,” you tell the browser.

Do Use IndexedDB When
  • IE 9 and under are not needed. (Safari can be satisfied with a polyfill)
  • Your data only has to leave in the browser, and doesn’t need to match a server-side schema.
  • You have an existing backend solution that has an IndexedDB layer, like CouchDB and PouchDB.
  • You are ready to use the newest and coolest that is also well supported and established.

Do Not Use IndexedDB When

  • Your data is very simple and easily mapped to keys (use localStorage)
  • Your data is coming from a SQL server and you essentially need to mirror it on the client (maybe use WebSQL)
  • You can use a higher-level or more featureful abstraction on top of the specific storage layer (like PouchDB)

Comments

Popular posts from this blog

CARDIAC: The Cardboard Computer

I am just so excited about this. CARDIAC. The Cardboard Computer. How cool is that? This piece of history is amazing and better than that: it is extremely accessible. This fantastic design was built in 1969 by David Hagelbarger at Bell Labs to explain what computers were to those who would otherwise have no exposure to them. Miraculously, the CARDIAC (CARDboard Interactive Aid to Computation) was able to actually function as a slow and rudimentary computer.  One of the most fascinating aspects of this gem is that at the time of its publication the scope it was able to demonstrate was actually useful in explaining what a computer was. Could you imagine trying to explain computers today with anything close to the CARDIAC? It had 100 memory locations and only ten instructions. The memory held signed 3-digit numbers (-999 through 999) and instructions could be encoded such that the first digit was the instruction and the second two digits were the address of memory to operate on

The Range of Content on Planet Python

I've gotten a number of requests lately to contribute only Python related material to the Planet Python feeds and to be honest these requests have both surprised and insulted me, but they've continued. I am pretty sure they've come from a very small number of people, but they have become consistent. This is probably because of my current habit of writing about NaNoWriMo every day and those who aren't interested not looking forward to having the rest of the month reading about my novel. Planet Python will be getting a feed of only relevant posts in the future, but I'm going to be honest: I am kind of upset about it. I don't care if anyone thinks it is unreasonable of me to be upset about it, because the truth is Planet Python means something to me. It was probably the first thing I did that I considered "being part of the community" when I submitted my meager RSS feed to be added some seven years ago. My blog and my name on the list of authors at Plan

Pythonic Defined

Introduction Losing is Good Strings Dictionaries Conclusion Introduction Veterans and novices alike of Python will hear the term "pythonic" thrown around, and even a number of the veterans don't know what it means. There are times I do not know what it means, but that doesn't mean I can define a pretty good idea of what "pythonic" really means. Now, it has been defined at times as being whatever the BDFL decides, but we'll pull that out of the picture. I want to talk about what the word means for us today, and how it applied to what we do in the real world. Languages have their strengths and their idioms (ways of doing things), and when you exploit those you embrace the heart of that language. You can often tell when a programmer writing in one language is actually more comfortable with another, because the code they right is telltale of the other language. Java developers are notorious for writing Java in every language they get their hands on. Ho