简体   繁体   English

测试localStorage / sessionStorage最大数据量

[英]Testing localStorage/sessionStorage max data amount

I'm writing a page to test how much data localStorage and sessionStorage can save in the browser. 我正在写一个页面来测试localStorage和sessionStorage可以在浏览器中保存多少数据。

It works somewhat but the browser gets unresponsive and the process bar/text is not updated progressively but mostly all at onces at the end or when the unresponsive dialogbox apears. 它可以工作,但浏览器无法响应,并且进程栏/文本不会逐步更新,而是在结束时或无响应的对话框消失时立即全部更新。

One of the reasons for the unresponsiveness if when i create a really large string. 如果我创建一个非常大的字符串,则无响应的原因之一。

  blablaantal might be 1048576 to create a string with 1048576 x-charachers.

  data = '';

  for (i = 0; i < blablaantal; i++) {

     data += 'x';

  }

Code and Demo : http://netkoder.dk/netkoder/eksempler/eksempel0008.html 代码和演示: http : //netkoder.dk/netkoder/eksempler/eksempel0008.html

localStorage.remainingSpace will tell you how many bytes you can store. localStorage.remainingSpace会告诉您可以存储多少字节。

EDIT: In a more general case, try this: 编辑:在更一般的情况下,请尝试以下操作:

blablaantal = 1048576;
data = new Array(blablaantal+1).join("x");

Yeah, i played around a few years ago to see if i could find a safe, performant way to test this in all browsers. 是的,我大约在几年前就玩过,看看是否可以找到一种安全,高效的方式在所有浏览器中对此进行测试。 I didn't. 我没有 The best i achieved at the time is here: 我当时取得的最好成绩是:

https://github.com/nbubna/store/blob/master/src/store.measure.js https://github.com/nbubna/store/blob/master/src/store.measure.js

as a plugin for my store2.js wrapper. 作为我的store2.js包装器的插件。 It'll work faster than your method, but it will still crash/freeze things up sometimes. 它的工作速度比您的方法快,但有时仍然会崩溃/冻结。 Though, i haven't tried in a few years, things may be better or, i suppose, worse. 虽然,我几年没有尝试过,但情况可能会好些,或者,我想可能会更糟。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM