Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Localstorage Vs Cookies - Performance

Since cookies are server-side and Localstorage is client-side, which is the fastest for the user to retrieve? (ref. Local Storage vs Cookies)

I assume if the client's machine is slow, then cookies are faster? Or that makes no diffrence?

I am using both localstorage & cookies for a project and both are retrieved using jQuery. That means, jquery has to get loaded, then the data is being retrieved.

How can I make this faster? I don't know how both work. For example, some say that Cookies are being retrieved once the HTML is trying to load before the style's and js files and others say when DOM is ready.

Does anyone know accurate details about which is faster for the user to retrieve?

Thank you

like image 798
jQuerybeast Avatar asked Oct 17 '11 21:10

jQuerybeast


People also ask

Which is faster local storage or cookies?

Both cookies and LocalStorage are stored locally on the client. There should be no noticeable difference in how quickly you can read from them. Cookies do have the disadvantage of being transmitted to and from the server on HTTP requests and responses. This may increase the page size, increasing page load time.

How is localStorage better than cookies?

Cookies are intended to be read by the server, whereas localStorage can only be read by the browser. Thus, cookies are restricted to small data volumes, while localStorage can store more data.

What is the big difference between local storage and cookies?

The major difference between cookies and local storage is the amount of data that can be stored. In cookies, the limit is 4MB; however, you can store up to 5MB of data in local storage, which is huge. It allows writing a lot of content in local storage to use on the website. Similar to local storage is session storage.

Does localStorage count as cookies?

Cookies are just one type of local storage, but there are others. It is important that you know them so you can block them or delete the data stored by them: Browser local storage.

What is the difference between cookies vs local storage vs session storage?

Cookies vs Local Storage vs Session Storage 1 Has different expiration dates (both the server or client can set up expiration date) 2 The Client can't access the Cookies if the HttpOnly flag is true 3 Has SSL Support 4 Data are transferred on each HTTP request 5 4kb limit More ...

How long does local storage data stay in cookies?

doesn't have expiry date so if you forgot to delete Local Storage data, Local Storage data can stay forever. is about 5MB as a common size (depending on browsers). I recommend using Cookie for sensitive data and Local Storage for non-sensitive data. Highly active question.

Should you use cookies or localStorage for web development?

While these storage options have their positives and negatives, they both have applications in modern web development. Cookies are smaller and send server information back with every HTTP request, while LocalStorage is larger and can hold information on the client side.

What is the local storage in a browser?

The Local Storage is a type of Web storage which like cookies is accessible on all windows in the browser. When it comes to the storage capacity, it can store upto 5–10 MB, which is much better when compared to cookies. And the values set in local storage never expires until and unless we manually remove them.


2 Answers

Since cookies are server-side and Localstorage is client-side, which is the fastest for the user to retrieve?

You're starting off with an incorrect assumption. Cookies are stored client-side as well. Both localStorage and cookies are stored client side.

The difference however, and that cookies can be set, manipulated from the server side. localStorage is only workable from the client side.

I assume if the client's machine is slow, then cookies are faster? Or that makes no diffrence [sic]?

They are such lightweight operations I wouldn't worry about speed.

Performance between the two is not a factor for choosing one. It's what are your needs.

Cookies:

  1. Have worked in browsers for a long time. Every modern, and not so modern, browser support cookies.
  2. Can be accessed and manipulated server side.
  3. Are typically limited to a few kilobytes, depending on browser.

localStorage:

  1. Is a relatively new concept. Not all browsers support it. IE 6, 7 for example, and you're out of luck.
  2. Is inaccessible from the server side. You could make an AJAX call back to the server with data that is stored in localStorage.
  3. Have a W3C recommendation of being able to store 5 MB.

How can I make this faster?

I don't think either one can directly introduce a performance problem. Other problems, like waiting for the DOM to be in ready state, waiting for script files to load; etc. can introduce slowdowns.

like image 115
vcsjones Avatar answered Sep 30 '22 06:09

vcsjones


Both cookies and LocalStorage are stored locally on the client. There should be no noticeable difference in how quickly you can read from them.

Cookies do have the disadvantage of being transmitted to and from the server on HTTP requests and responses. This may increase the page size, increasing page load time.

If you are using jQuery to read cookies/LocalStorage, then the browser will have to wait for the jQuery script to be downloaded if it isn't in the browser's cache.

like image 34
Greg Avatar answered Sep 30 '22 06:09

Greg