Yet another intro for localStorage and sessionStorage
As we know, localStorage and sessionStorage have came up for years. They're commonly used in front-end cache for both online and offline situations, like, storing JWT.
I suppose you have been way familar with them already, so here's just a quick note for myself. If it's also your jam, stay tuned!
Why we need additional storage objects besides cookies?
Although cookie is capable of storing in client-side, but there are some downsides where the web storage objects have value of.
- Unlike cookies, web storage objects will not be sent along with every single request. There is no much extra workload, even if we store dozens of data by web storage objects.
- Most browsers allow at least two megabytes or more (have to configure by user themselves) for web storage objects, so we could have better performance by cache more.
- The data stored in web storage object can survive from page refresh and even a full browser or OS restart.
setItem(key: string, value: any): void
store a key/value pair.getItem(key: string): string
get value by key.removeItem(key: string): void
remove a key with its value.clear(): void
delete everything.key(index: number): string
get the key on given position.length: number
get the amount of key/value pairs.
setItem
method would be converted to a string automatically before store, that means it will call the toString
method of the given value first, and then save the returned value finally. If you want to put an object in, call JSON.stringify
first, and then execute JSON.parse
when get it back.And it's possible to stringify the whole storage object, e.g. for debugging purposes:
// add formatting options to JSON.stringify to make the object look prettier.
JSON.stringify(localStorage, null, 2)
What's the difference between them?
- 【Yet another intro for localStorage and sessionStorage】The ability for cross-context communication:
- SessionStorage is scoped on per-window basis, that means multiple browser tabs with documents on the same origin have a separated sessionStorage, but iframes on the same origin within the same window/tab can share the sessionStorage.
- LocalStorage can be shared between all browsing-contexts(iframe, window and tabs) and workers.
- The expiration machinsm:
- Data stored in sessionStorage will expire automatically when close the browser tab.
- Data persisted in localStrorage will live as long as it could until user perform data clearence.
Here's the properties of
StorageEvent
:key
the key that was changed,null
ifclear()
is called.oldValue
the old value,null
if the key is newly added.newValue
the new value,null
if the key is deleted.url
the url of the ducument where the change happend.storageArea
eitherlocalStorage
orsessionStorage
object where the change occured.
// a.html
window.addEventListener('storage', evt => {
console.log('a:', evt.key)
})
setTimeout(()=> {
localStorage.setItem('a', 1)
}, 5000)// b.html
window.addEventListener('storage', evt => {
console.log('b:', evt.key)
})
setTimeout(()=> {
localStorage.setItem('b', 1)
}, 10000)
Console for a.html will show
a:b
after ten seconds, and that of b.html will echo b:a
in five seconds.推荐阅读
- 人工智能|hugginface-introduction 案例介绍
- introduction
- R语言数据建模流程分析
- 一条HTTP请求的生命周期(一)-- HTTP/1.1
- 数学思维导论(一) Introduction to Mathematical Thinking 什么是数学(为什么要学习数学?)
- 【以太坊】使用MyEtherWallet部署ICO合同
- myEtherWallet在线钱包的使用
- 【以太坊钱包开发|【以太坊钱包开发 二】MyEtherWallet 钱包介绍
- MyEtherWallet使用教程
- Blockchian|『0001』 - 如何通过 MyEtherWallet 创建钱包以及如何通过 Ethereum Wallet 和 MetaMask 恢复钱包账号