Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

localStorage 性能测试 #45

Open
kangschampagne opened this issue Oct 22, 2018 · 0 comments
Open

localStorage 性能测试 #45

kangschampagne opened this issue Oct 22, 2018 · 0 comments

Comments

@kangschampagne
Copy link
Owner

@kangschampagne kangschampagne commented Oct 22, 2018

前言

记录之前对 localStorage 性能相关的测试例子。

1.存 单key/多key 的性能差别

直接上代码:

const useNow = false

const data = []

for (let i = 0, l = 100000; i < l; i++) {
  data.push(Math.random() * 100000 | 0)
}

function task1(done) {
  const secondBegin = useNow ? performance.now() : +new Date
  const tmp = {}
  for (let i = 0, l = data.length; i < l; i++) {
    tmp[`data-${i}`] = data[i]
  }
  localStorage['data'] = tmp
  const secondEnd = useNow ? performance.now() : +new Date
  console.log('存在单个key中', secondEnd - secondBegin)
  done && done()
}

function task2(done) {
  const firstBegin = useNow ? performance.now() : +new Date
  for (let i = 0, l = data.length; i < l; i++) {
    localStorage[`data-${i}`] = data[i]
  }
  const firstEnd = useNow ? performance.now() : +new Date
  console.log('存在多个key中', firstEnd - firstBegin)
  done && done()
}

setTimeout(() => {
  task1(() => {
    setTimeout(() => {
      task2()
    }, 100)
  })
}, 100)

可以直接复制放在控制台中,直接试一试当前浏览器的localStorage,这边直接给一个结果。

测试结果: 在 localStorage 存放同一份数据,单key存储的时间大于多key存储的时间。

更多测试

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant
You can’t perform that action at this time.