-3

I am creating a project where I want to save the last level that the user reached so they can continue where they leave off the next time. I am wondering whether I should use cookies or web storage, and the reasons for using each one. For example, does one affect performance, is one commonly enabled/disabled, is one not supported by modern browsers, etc.

I am also interested in knowing whether there are other options besides these two that I could use instead, and what the advantages and disadvantages to those are.

Thank you!

Fluzz
  • 68
  • 12

2 Answers2

1

**Cookies**
Pros
1. Legacy support (it's been around forever)
2. Persistent data
3. Expiration dates

Cons
1. Each domain stores all its cookies in a single string, which can make parsing data difficult
2. Data is unencrypted.
3. small in size, cookies are sent with every HTTP request
4. Limited size (4KB)
5. SQL injection can be performed from a cookie

**Local storage / Web storage**
Pros
1. Support by most modern browsers
2. Persistent data that is...
3. stored directly in the browser
4. Same-origin rules apply to local storage data
5. Is not sent with every HTTP request
6. ~5MB storage per domain (that's 5120KB)

Cons
Not supported by anything before:
1. IE 8
2. Firefox 3.5
3. Safari 4
4. Chrome 4
5. Opera 10.5
6. iOS 2.0
7. Android 2.0
8. If the server needs stored client information you purposefully have to send it. [3]



Based on my comparison I think that local storage is the logical successor of cookies for storing client-side data. While cookies were originally created as a work-around, local storage has been designed with a purpose.

Local storage is easier to work with because you don't have to parse a string to get data. Instead you can make a call for the variable name and it's value is returned. The same applies with creating and deleting data as well.

For saving client-side data I believe local storage wins. As someone on the stackoverflow boards pointed out however, if your server needs information from a client cookies will be a better solution. But if you just want to save user-prefs, num vists, or other stuff like that choose local storage.
0

They have both completely different purposes. Cookies are available from the server, while Web Storage is available for the client only. Each request, cookies will be send to the server with the HTTP header. This is not needed when the server doesn't need the data. In that case Web Storage is probably the way to go.

It all depends on who you want to have access to the data.

Web Storage offers a way to permanently store data (until the user clears its browser cache), while cookies are set to expire after a certain time.

Web Storage is also more suitable to store big chunks of data, like full JSON representations of data (up to 5MB). Web Storage is supported starting from IE8 and up, so that should be no problem.

Rein
  • 3,211
  • 3
  • 16
  • 21