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
1 changed files with 7 additions and 13 deletions

View File

@ -8,19 +8,13 @@ const hasLocalStorage = testHasLocalStorageOnce()
export const storeLite = {
get () {
if (!hasLocalStorage) {
return {}
}
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 new Proxy({}, {
get: function (obj, prop) {
if (!(prop in obj)) {
obj[prop] = hasLocalStorage && safeParse(localStorage.getItem(`store_${prop}`))
}
return obj[prop]
}
}
return res
})
}
}