4 ms·
Storing Data the Simple HTML5 Way (and a few tricks you might not have known)
- y3di 15y agoSo should local storage be used as a better alternative to cookies?
- sp332 15y agoCookies are shared with the server. Local storage generally is for local data only.
- daleharvey 15y agoJust to expand on that, the sharing between the server doesnt just have privacy implication, it also means a whole ton of data that gets transferred with every request (including image / css requests etc)
- a3camero 15y agoA gotcha that's not listed is the limit on characters that you can store using localStorage: http://arty.name/localstorage.html http://arty.name/localstorage.html
- arethuza 15y agoThat's a nice test - but from what I can see it is storing a single value in localStorage that gets larger and larger. I wonder if the behaviour is different if you have multiple smaller documents?
- deleted 15y ago[deleted]
- a3camero 15y agoThere's a fixed MB limit on most browsers/devices I think. I came across this after experiencing problems saving files to PlayBook using HTML5. The main problem seems to be size but there's also a limit on how many items you can write I think. Didn't test it too thoroughly, came up with workaround involving splitting files.
- misterbwong 15y agoFYI for anyone clicking that link-it crashes Chrome on my machine (version 13.0.782.112)
- rufibarbatus 15y ago13.0.782.112 m (on Windows) here, didn't crash but experienced a slowdown.
- CrabDude 15y agoI’d add that the localStorage setter (localStorage.item = ‘someValue’) is up to 2x faster than setItem: http://jsperf.com/localstorage-getitem-setitem-vs-getter-setter http://jsperf.com/localstorage-getitem-setitem-vs-getter-set... Also, a small gotcha some may face is: typeof localStorage.undefinedItem === “undefined” localStorage.getItem(‘undefinedItem’) === null Additionally, since WebKit’s localStorage stores strings encoded as UTF-16, it only holds 2.5MB worth of UTF-8 encoded strings, compared to the 5MB of other non-WebKit browsers. http://code.google.com/p/chromium/issues/detail?id=58985#hc15 http://code.google.com/p/chromium/issues/detail?id=58985#hc1... http://stackoverflow.com/questions/2747285/html5-localstorage-restrictions-and-limits http://stackoverflow.com/questions/2747285/html5-localstorag... http://jsfiddle.net/brucealdridge/j6KdJ/ http://jsfiddle.net/brucealdridge/j6KdJ/ Check for the “QUOTA_EXCEEDED_ERR” exception in this case: http://dev.w3.org/html5/webstorage/#the-storage-interface http://dev.w3.org/html5/webstorage/#the-storage-interface Additionally, as stated in the chromium issue above’s comments, localStorage performance degrades as size increases and is considered slower than IndexedDB, though with the benefit of a FAR SIMPLER API and broader browser support (IE8, FF3.5, S4, Chr4, O10.5). https://developer.mozilla.org/en/dom/storage#Browser_Compatibility https://developer.mozilla.org/en/dom/storage#Browser_Compati...