perf: don't interate through all of localStorage in inline script (#1264)

This commit is contained in:
Nolan Lawson 2019-06-02 09:07:45 -07:00 committed by GitHub
parent 58a8772edc
commit 5d0e95e759
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -8,19 +8,13 @@ const hasLocalStorage = testHasLocalStorageOnce()
export const storeLite = {
get () {
if (!hasLocalStorage) {
return {}
return new Proxy({}, {
get: function (obj, prop) {
if (!(prop in obj)) {
obj[prop] = hasLocalStorage && safeParse(localStorage.getItem(`store_${prop}`))
}
const res = {}
const LS = localStorage
for (let i = 0, len = LS.length; i < len; i++) {
let key = LS.key(i)
if (key.startsWith('store_')) {
let item = LS.getItem(key)
let value = safeParse(item)
res[key] = value
}
}
return res
return obj[prop]
}
})
}
}