The difference between localStorage and cookies

cookie:

It is called session tracking technology, which is to track and record some states in a session. The cookie can track and record the status of the client (for example: whether to log in, shopping cart information, whether it has been downloaded, etc.) from the beginning to the end of a session.

"Session" refers to the process from the browser opening a website to visiting its other pages until the browser is closed.

Features of cookies:

can only store text

A single storage has a size limit of about 4KB

Quantity limit, general browser, the limit is about 50

Read is restricted by domain name

statute of limitations

path restriction


localStorage:

It is mainly used as local storage, which solves the problem of insufficient cookie storage space (the storage space of each cookie in cookie is 4k), and the general browser supports 5M size in localStorage, which is different in different browsers localStorage will vary.

Advantages of localStorage

1. localStorage expands the 4K limit of cookies

2. localStorage can directly store the data requested for the first time locally. This is equivalent to a 5M database for front-end pages. Compared with cookies, it can save bandwidth, but this is only available in high-version browsers. only supported in

Limitations of localStorage

1. The size of the browser is not uniform, and the IE version above IE8 only supports the property of localStorage

2. At present, all browsers will limit the value type of localStorage to string type, which requires some conversion for our daily common JSON object type (JSON.parse()//to an array) (JSON.stringify( ) // convert to string)

3. localStorage is not readable under the privacy mode of the browser

4. localStorage is essentially reading strings. If there is a lot of storage content, it will consume memory space and cause the page to become stuck.

5. localStorage cannot be crawled by crawlers

The only difference between localStorage and sessionStorage is that localStorage belongs to permanent storage, while sessionStorage belongs to when the session ends, the key-value pairs in sessionStorage will be cleared.

Guess you like

Origin blog.csdn.net/m0_52191385/article/details/130973398
Recommended