Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript API wrapper around web storage?

Is there a cross-browser jquery like wrapper API abstraction over the various web storage systems available?

like image 752
pathikrit Avatar asked Jul 28 '12 23:07

pathikrit


3 Answers

localStorage (aka "web storage") is not really comparable to Web SQL or IndexedDB because it is severely restricted in space and it is synchronous (read: freezes the UI of the browser as it does shit). Because of this, I'm ignoring localStorage in the rest of my post. But if you only need a little bit of storage, you can just use localStorage directly, as it is pretty widely supported, or use lawnchair as the other post suggested, which is even more widely supported.

So we're left with Web SQL and IndexedDB. Web SQL, for better or worse, is a dead end technology. Nobody is working on the spec, nobody is working on a new implementation. IE and Firefox will never support it, and it is deprecated in other browsers and will likely be removed eventually. But there are currently some browsers that support it (Chrome, Opera, Safari), so theoretically it could be used in a library if the other browsers that don't support Web SQL had some alternative.

IndexedDB unfortunately is not well supported across browsers. Firefox has a good implementation; Chrome's is lagging a bit but catching up. You can use this IndexedDB Polyfill to get IndexedDB support in any browser that supports Web SQL, which theoretically should cover all recent/popular browsers except IE.

Ah, IE. It always comes down to IE, doesn't it? Fact is, there is no reasonable way to do local storage like IndexedDB or Web SQL in IE. IE 10 will support IndexedDB. Whenever people stop using IE 9 and lower, IndexedDB will likely have superb support in all other browsers, so you'll just be able to use the IndexedDB API or some library on top of it. Until that point... well, you're out of luck.

like image 171
dumbmatter Avatar answered Nov 07 '22 14:11

dumbmatter


Wrappers Supporting multiple Storage Implementations

Pouch DB

localForage

lawnChair

YDN - DB

IndexedDB polyfill over WebSQL

IndexedDB Shim

IndexedDB Polyfill

If you just want to store small amounts of data any of these or simpler wrappers for just web storage will be enough. If you need more storage space you will need to drop the idea of web storage and use indexedDB polyfill. But the drawback with such polyfills is that indexing may not work well always especially with multiple indexes and can be of poor performance in mobile browsers without native indexedDB (iOS).Pouch DB looks like a good solution with its secondary indexes if indexing is crucial. If iOS is not a target you can just go forward with native indexed DB as its supported almost everywhere else outside apple gardens.


Web Storage

Mode: Key-Value pair Size: 2.5MB – 5MB Scalability: Poor performance for large/complex data Search: Poor search performance without indexes or other means Indexing: No Indexing available Compatibility: Supported in all modern browsers including mobile browsers Future Proof: Support should be maintained for very long time. The spec is separate from w3c and exists as a spec of its own. W3c is probably going to do everything with indexed DB


WebSQL

Mode: Relational databse Size: Default ~5MB asks user to expand to 10, 50, 100, 500 MB Scalability: Uses relational databse concepts so can be scaled well. Good performance for large data when compared to Web Storage. Search: Good search performance with indexing in RDBMS. Indexing: Indexing available in RDBMS Compatibility: Supported on webkit based browsers including Chrome, Safari and mobile browsers like iOS safari. Not supported by Microsoft and Firefox and have decided to never support as the spec depends on non-standard SQL lite. At the same time web SQL happens to be the only scalable solution available in iOS devices and Safari. Future Proof: Web SQL is not future proof. The specification is no more maintained and deprecated as of 2011. Also most vendors except Apple has started moving to indexed DB.


IndexedDB

Mode: Indexed table system Size: Default ~5-50MB asks user to expand. Scalability: Can have as many databses and as many stores per database. Can be scaled easily. Search: Very good search performance with technology rooted in indexing. Indexing: Design itself based on indexing. Compatibility: Supported in Chrome, Firefox, IE 10 among other modern browsers. The missing vendors are just Apple. Apple still haven’t implemented indexed DB on neither Safari nor its iOS Safari. Future Proof: The indexed DB specification is the current direction of web persistence designed particularly for client side storage. This should soon knock out web SQL and Local Storage as it has the power of both these specs.

like image 29
sabithpocker Avatar answered Nov 07 '22 12:11

sabithpocker


Store.js should work for you. It uses localStorage where available. For IE6 and IE7 it, uses userData behavior.

It's API is nice and simple:

store.set('myage', 24)
store.get('myage') === 24
like image 6
stevendaniels Avatar answered Nov 07 '22 13:11

stevendaniels