Question

I'm using AmplifyJS Store. I think it's a good wrapper for persistent storage. I've been using it in a JQuery plugin and it works pretty well.

Lately though, I've been wondering if I can just do the same thing using a namespace variable in the window object?!

AmplifyJS Store + JSON2.js (required for data serialization) costs me 22Kb (8Kb minified) of file size alone, not to mention the additional supporting code I've had to create around it. Also, I'm using only sessionStorage (i.e. I have no need for persistence after the browser window closes, only while the window is active).

So, is there really any major reason I can't use the window object instead of AmplifyJS Store for my specific circumstance?! I've thought about the expiry feature but I can easily build in same functionality in a few lines of code.

Thanks.

Was it helpful?

Solution 2

If you are actually trying to save persistent data (data that is still there if the page reloads or the user navigates away), storing it on the window object is not an option. The data won't be there when you check for it later.

OTHER TIPS

HTML5 localStorage in window object by itslef has a very simple api:

 var valueOfName = window.localStorage.getItem("name");
 window.localStorage.setItem("name", "value");

you can even omit global window object:

 var valueOfName = localStorage.getItem("name");
 localStorage.setItem("name", "val");

moreover you can apply array-style notation:

 var valueOfName = localStorage["name"];
 localStorage["name"] = "value";

that is it! And it doesn't have an expire date, URL strings or other complications in its API which took place in elder cookies-approach. All what AmplifyJs provides (as I can see) is a support for older browsers (who were using cookies) by givin' you the same API as original localStorage does.

In other words if you are not targeted on Netscape Navigator, Mosaic and IE 7 you can forget about using of AmplifyJS and apply native localStorage API.

However despite an approach, you should never rely on client-side persistance for sure, because it is totally dependent from local browser (client could simply clear cache, reinstall something or sign-in from other computer) - use server-side databases and similiar technologies for saving user's info.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top