Hacker News new | ask | show | jobs
by homakov 3401 days ago
localStorage is pretty much only thing we need these days.
2 comments

Almost! Except if you want to store objects/arrays/numbers (localStorage only supports strings), or want your site to work in Safari Private mode (where localStorage breaks), or if you have to support legacy environments with old browsers (like many government services), etc etc etc :)
Not that I disagree with your other arguments but this one sticks out:

> Except if you want to store objects/arrays/numbers (localStorage only supports strings)

My normal usage of localStorage usually looks like this:

    const getItem = (item) => { JSON.parse(window.localStorage.getItem(item)) }
    // and
    const setItem = (item, val) { window.localStorage.setItem(item, JSON.stringify(val)) }
And then you won't have issues with any type.
Except when a value has already been stored previously without JSON.stringify (in which case JSON.parse will throw), or you need a default value if unset (e.g `store.get('username', 'anonymous')`, or ... :) But I also see your point that sometimes the very quick and simple solution can be useful!
Not if you want to store more than few MB (5 or 10 max depending of browsers if I recall). Not if you want/need an async API. Not if you need to be able to search your storage.
Yup on all accounts, although: > Not if you want to store more than few MB (5 or 10 max depending of browsers if I recall)

There's a case for something like http://pieroxy.net/blog/pages/lz-string/index.html

which can save up a lot of space

Let's create a plugin that automatically does compression!
Created issue: https://github.com/marcuswestin/store.js/issues/183

Will tackle a bit down the road.