Is browser local storage API better than cookies?

Foxhound5366

Honorary Master
Joined
Oct 23, 2014
Messages
11,284
Reaction score
5,004
Location
Big Smoke
Wait long enough to teach yourself something, and a newer and better technology comes along lol.

I've been meaning to teach myself how to use JavaScript cookies for years, and when I finally googled it today I learned that the new local storage API is way better and more intuitive.

I built a quick test and it sure works for persistent memory of simple name/value pairs, which is all I wanted.

For the more experienced coders here, is this considered the new "best practice" way of storing data quickly in the local browser beyond the current session, or is there something to beware of?
 
Cookies and local storage are related in that they are used in a browser, but they are not competing technologies.

The max size of a cookie is 4096bytes, local storage 5MB, depending on the browser.

Most front end framework state management libraries have a way to store state in local storage to survive a screen refresh.

So yes, local storage can be very useful.
 
Cookies and local storage are related in that they are used in a browser, but they are not competing technologies.

The max size of a cookie is 4096bytes, local storage 5MB, depending on the browser.

Most front end framework state management libraries have a way to store state in local storage to survive a screen refresh.

So yes, local storage can be very useful.
Well, just seems easier to use local storage. You save with one simple command, you read with another simple command ... and I read something about local storage being better than cookies in situations where users have multiple browser windows open on the same website (quite common these days).

So seeming as people seem shy to answer the question as asked, let's try the inverse: are there any situations where cookies are preferable to local storage?
 
Yes, it’s preferable to use cookies to store cookie stuff, like session id’s, tokens.

And it’s preferable to store application/business data in local storage.

Cookies can expire.
Local storage does not.

Both use simple commands to write and read
 
Last edited:
Yes, it’s preferable to use cookies to store cookie stuff, like session id’s, tokens.

And it’s preferable to store application/business data in local storage.

Cookies can expire.
Local storage does not.

Both use simple commands to write and read
Wait till he finds out about sessionstorage :)
 
Wait till he finds out about sessionstorage :)
You know, MagNorthDigital, I'll show you a courtesy you didn't show me and I'll address you directly. Because context is important, right, and this IS a thread where I asked experienced coders on MyBB to help me.

It was a fairly simple brief, but one which you decided to ignore completely. I mean, myBB has a lot of very helpful communities ... the gun community, the gaming community, the braai community ... isn't it ironic that the development community on here has toxic members such as yourself?

And yes, MagNorthDigital, you were toxic. I can only wonder whether you're a big enough douche to make snide comments like that in real life, in front of the people asking innocently for help ... because that's the digital version of what you just did.

So thanks for nothing. Thanks for failing this MyBB community. And for the record, I DID know about session storage, but seeing as I am asking about two forms of persistent storage we can safely conclude that I am not interested in that. But actually being helpful or even on-topic was never on your agenda.
 
You know, MagNorthDigital, I'll show you a courtesy you didn't show me and I'll address you directly. Because context is important, right, and this IS a thread where I asked experienced coders on MyBB to help me.

It was a fairly simple brief, but one which you decided to ignore completely. I mean, myBB has a lot of very helpful communities ... the gun community, the gaming community, the braai community ... isn't it ironic that the development community on here has toxic members such as yourself?

And yes, MagNorthDigital, you were toxic. I can only wonder whether you're a big enough douche to make snide comments like that in real life, in front of the people asking innocently for help ... because that's the digital version of what you just did.

So thanks for nothing. Thanks for failing this MyBB community. And for the record, I DID know about session storage, but seeing as I am asking about two forms of persistent storage we can safely conclude that I am not interested in that. But actually being helpful or even on-topic was never on your agenda.
Have you read your profile picture, lately?
 
All good, I'm sure there are some puppies for you to kick somewhere, to prove you're real edgy. Seeing as you can't deliver useful advice when asked.

NjIuanBn
 
Yes, it’s preferable to use cookies to store cookie stuff, like session id’s, tokens.

And it’s preferable to store application/business data in local storage.

Cookies can expire.
Local storage does not.

Both use simple commands to write and read
No server side implementation should be dependent on cookies expiring. Its a more specific and in depth choice. Use either one really. This will depend on your application/infrastructure/goals/security/ux etc

One important and practical CS "rule of thumb/principle" is, "there are no silver bullets". Each choice comes with its upsides and corresponding downsides. It's damn near impossible to say if one approach is "better" than the other without a fuller picture. Even then, there will always be trade offs.
 
Last edited:
No server side implementation should be dependent on cookies expiring. Its a more specific and in depth choice. Use either one really. This will depend on your application/infrastructure/goals/security/ux etc

One important and practical CS "rule of thumb/principle" is, "there are no silver bullets". Each choice comes with its upsides and corresponding downsides. It's damn near impossible to say if one approach is "better" than the other without a fuller picture. Even then, there will always be trade offs.
Of course not, no one said it should.
Expiration is just a feature of cookies.

I’ve never come across this “trade off” thing before. We should have a thread about the trade offs of using a cookie for storing app data vs local storage
 
Of course not, no one said it should.
Expiration is just a feature of cookies.

I’ve never come across this “trade off” thing before. We should have a thread about the trade offs of using a cookie for storing app data vs local storage
It pretty much applies to every aspect of CS. Here's the relevant source for reference.

Essentially every choice an engineer makes is a balance of pros and cons. There's not necessarily a fundamentally "better" choice between the two in this instance.
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X