Jul 1, 2013

Should I use Cookies or Web Storage?

Cookies and Web Storage serve similar purpose in some and different purpose in many ways. Both cookies and web storage stores data on the client but cookies are sent back to the server on every request. So, if the data is needed to be used only by the client - then, its better to use web storage. However, if the data needs to be used by the server then cookies is the way. For example, for sessions, cookies is the definitely needed. Cookies, like web storage, can be accessed fom client side as well using javascript.

One more thing to note is that not all browsers support web storage yet - for example, Internet explorer 7-, Firefox 3.5- and Chrome 4- does no not support web storage. So, web storage may not be a solution on all situations.

Also, web storage can have maximum storage of 5MB whereas cookies have a maximum storage of 4KB.

Cookies can also have an expiration date - that is, it expires after some time period. On the other hand, web storage is of 2 types - local and session. Session storage is really like local storage except that it expires when the session ends.

Should I use Cookies or Web Storage?

Cookies and Web Storage serve similar purpose in some and different purpose in many ways. Both cookies and web storage stores data on the client but cookies are sent back to the server on every request. So, if the data is needed to be used only by the client - then, its better to use web storage. However, if the data needs to be used by the server then cookies is the way. For example, for sessions, cookies is the definitely needed. Cookies, like web storage, can be accessed fom client side as well using javascript.

One more thing to note is that not all browsers support web storage yet - for example, Internet explorer 7-, Firefox 3.5- and Chrome 4- does no not support web storage. So, web storage may not be a solution on all situations.

Also, web storage can have maximum storage of 5MB whereas cookies have a maximum storage of 4KB.

Cookies can also have an expiration date - that is, it expires after some time period. On the other hand, web storage is of 2 types - local and session. Session storage is really like local storage except that it expires when the session ends.