Local storage is divided into the browser Cookie
, WebStorage
and IndexedDB
which WebStorage
can be divided into localStorage
and sessionStorage
. Next we come to one analysis of these local storage solutions.
Cookie
Cookie
Initially it was designed to do is actually not stored locally, but to make up for HTTP
the lack of state management .
HTTP
Protocol is a stateless protocol, a client sent a request to the server, the server returns a response, the story ended this way, but his next request to let the server know how the client who is it?
Against this background, it creates Cookie
.
A small text file on the Cookie is essentially a browser inside the store, inside a way to store key-value pairs (in chrome developer panel Application
you can see this column). Sends the request under the same domain name, will carry the same Cookie, server parses get Cookie, able to get the status of the client.
Cookie's role is well understood, is used for state storage , but it also has many fatal defects:
-
Capacity defect. The upper limit of the volume Cookie only
4KB
, used to store only a small amount of information. -
Performance deficiencies. Cookie domain name followed, regardless of the domain name following a certain address or need the Cookie, the request will carry the complete Cookie, so with the increase in the number of requests, in fact, will cause a huge waste of performance, because the request carries a lot of unnecessary Content.
-
Security flaws. Because Cookie passed through as plain text in the browser and the server, it can easily be intercepted by unauthorized users, and then a series of tampering, re-sent to the server in the Cookie of validity, which is quite dangerous. Further, in
HttpOnly
the case of the false, Cookie information can be directly read by the JS script.
localStorage
And Cookie similarities and differences
localStorage
One thing with Cookie
the same, that is, for a domain name, ie under the same name, the same will be stored for a period localStorage .
But it is relatively Cookie
still quite a few differences:
-
capacity. localStorage maximum capacity of 5M , compared to
Cookie
the 4K greatly increased. Of course, this is 5M for a domain name, so for a domain name are persistent. -
There is only the client, default is not involved in communication with the server-side. This will bring good to avoid the Cookie of performance problems and security issues .
-
Interface package. By
localStorage
exposure to a global, and through itsetItem
andgetItem
operating methods, etc., it is very convenient.
Operation method
Next we look at the specific how to operate localStorage
.
let obj = { name: "sanyuan", age: 18 }; localStorage.setItem("name", "sanyuan"); localStorage.setItem("info", JSON.stringify(obj)); 复制代码
Then the corresponding values are able to get into the same domain:
let name = localStorage.getItem("name");
let info = JSON.parse(localStorage.getItem("info")); 复制代码
As can be seen here, localStorage
in fact, are stored in the string, if it is stored in the object need to call JSON
the stringify
method, and dried JSON.parse
to resolve into objects.
Scenarios
The use of localStorage
large capacity and durable characteristics, can be used localStorage
to store some content stable resources, such as the official website logo
, the storage Base64
format of image resources, so uselocalStorage
sessionStorage
Feature
sessionStorage
The following terms and localStorage
consistent:
- capacity. Maximum capacity but also for the 5M.
- There is only the client, default is not involved in communication with the server-side.
- Interface package. In addition to
sessionStorage
the name change, storage, and mode of operation arelocalStorage
the same.
But sessionStorage
, and localStorage
there is an essential difference is that the former only session-level storage is not persistent storage. Session ends, that is, the page is closed, this part sessionStorage
ceased to exist.
Scenarios
- It can be maintained with the form information will be on the inside, you can ensure that the form information storage refresh the page even before the form information does not make the loss.
- You can use it to store this history. If you close the page without these records, with
sessionStorage
very appropriate. In fact microblogging to take such storage.
IndexedDB
IndexedDB
It is running in the browser 非关系型数据库
, essentially a database, and by no means just WebStorage 5M of an order of magnitude, this capacity is theoretically no limit.
On its use, this article focuses on the principles and tutorial documentation on MDN has very detailed here do not go into details, interested can look at the use of the document .
Then we analyze IndexedDB
some important features, in addition to the database itself has characteristics, such as 支持事务
, , 存储二进制数据
and so some features need extra attention:
- Key-value pair storage. Internal use
对象仓库
to store data, the data object in the repository using the key on the way to memory. - Asynchronous operation. Database belonging write I / O operations, the browser provides support for asynchronous I / O.
- By the same origin policy restrictions that can not access the cross-domain database.
to sum up
Browser development of various local storage and caching techniques, to front-end applications bring a lot of opportunities, PWA is also the basis of these excellent storage solution was able to develop. Re-comb the local storage solutions:
cookie
Not suitable for storage, and there is a lot of flaws.Web Storage
IncludinglocalStorage
andsessionStorage
, and will not participate in the default communications server.IndexedDB
Non-relational databases to run on the browser, providing an interface to store large data.