如何查看localStorage的大小
我目前正在开发一个将利用 HTML5 的 localStorage 的网站。我已阅读有关不同浏览器的大小限制的所有内容。但是,我还没有看到任何有关如何查找 localStorage 实例的当前大小的内容。 这个问题似乎表明JavaScript没有内置的方式来显示给定的大小多变的。 localStorage 是否有我没见过的内存大小属性?有没有一种简单的方法可以做到我所缺少的?
我的网站旨在允许用户在“离线”模式下输入信息,因此能够在存储即将满时向他们发出警告非常重要。
I am currently developing a site that will make use of HTML5's localStorage. I've read all about the size limitations for different browsers. However, I haven't seen anything on how to find out the current size of a localStorage instance. This question seems to indicate that JavaScript doesn't have a built in way of showing the size for a given variable. Does localStorage have a memory size property that I haven't seen? Is there an easy way to do this that I'm missing?
My site is meant to allow users to enter information in an 'offline' mode, so being able to give them a warning when the storage is almost full is very important.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(15)
我会使用 @tennisgen 的代码来获取所有内容并计算内容,但我自己计算键:
I would use the code of @tennisgen which get all and count the content, but I count the keys themselves:
除了这里投票最多的@serge 的答案之外,还需要考虑密钥的大小。下面的代码将添加存储在
localStorage
中的密钥的大小In addition to @serge's answer which is most voted here, size of the keys need to be considered. Code below will add the size of the keys stored in
localStorage
按照规范,字符串的每个字符都是 16 位。
但是用chrome检查(设置>内容设置>Cookies和站点数据)显示启动localStorage需要3kB(开销大小)
并且存储的数据大小遵循这个关系(精确到1kB)
3 + ((localStorage.x.length*16)/(8*1024)) kB
其中 localStorage.x 是您的存储字符串。
As the spec goes, each character of a string is 16 bit.
But inspecting with chrome (Settings>Content Settings>Cookies & Site data) shows us that initiating localStorage takes 3kB (overhead size)
And stored data size follows this relation (accurate to 1kB)
3 + ((localStorage.x.length*16)/(8*1024)) kB
where localStorage.x is your storage string.
是的,这个问题10年前就有人问过。但对于那些感兴趣的人(比如我自己,因为我正在构建一个离线文本编辑器,用本地存储保存数据)并且不擅长编程,您可以使用像这样简单的东西:
获取存储量填充的最佳方法是查看站点设置(例如,如果您将图像存储在本地存储中)。至少在chrome中,您可以看到使用的字节数(即:1222字节)。然而,上面已经提到了用js查看本地存储已满的最佳方法,所以使用它们。
Yes, this question was asked like 10 years ago. But for those interested (like myself, as I am building an offline text editor that saves data with local storage) and suck at programming, you could use something simple like this:
The best way to get the amount of storage filled is to view the site settings (say, if you stored an image in local storage). At least in chrome, you can see the amount of bytes used (ie: 1222 bytes). However, the best ways to see filled local storage with js have already been mentioned above, so use them.
我只是在页面加载后或在任何输入焦点处使用带有一些测试内容的 try/catch,例如:
I would just use a try/catch with some test content after the page loads or at any input focus, like:
//键和值都占用内存,因此更新了代码。
//Memory occupy by both key and value so Updated Code.
在 JavaScript 控制台中执行此代码片段(一行版本):
为了阅读方便,将多行相同的代码
添加到书签的“位置”字段中以方便
使用片段根据评论中的要求进行更新。现在计算包括密钥本身的长度。
每个长度都乘以 2,因为 javascript 中的 char 存储为 UTF-16(占用 2 个字节)
PPS 应该在 Chrome 和 Firefox 中工作。
Execute this snippet in JavaScript console (one line version):
The same code in multiple lines for reading sake
or add this text in the field 'location' of a bookmark for convenient usage
P.S. Snippets are updated according to request in the comment. Now the calculation includes the length of the key itself.
Each length is multiplied by 2 because the char in javascript stores as UTF-16 (occupies 2 bytes)
P.P.S. Should work both in Chrome and Firefox.
根据 @Shourav 上面所说的,我编写了一个小函数,它应该准确地获取所有
localStorage
键(对于当前域)并计算组合大小,以便您确切知道有多少内存由您的localStorage
对象占用:我的返回:
“30.896484375 KB”
Going off of what @Shourav said above, I wrote a small function that should accurately grab all your the
localStorage
keys (for the current domain) and calculate the combined size so that you know exactly how much memory is taken up by yourlocalStorage
object:Mine returned:
"30.896484375 KB"
您可以使用 Blob 功能。 这可能在旧浏览器中不起作用,请检查对
新 Blob 的支持
和Object.values()
在卡尼乌斯。示例:
Object.values() 将 localStorage 对象转换为数组。 Blob 将数组转换为原始数据。
You can get the current size of the local storage data using the Blob function. This may not work in old browsers, check the support for
new Blob
andObject.values()
at caniuse.Example:
Object.values() turns the localStorage object to an array. Blob turns the array into raw data.
IE 有一个 remainingSpace 属性存储对象。目前其他浏览器还没有类似的功能。
我相信默认的空间量是 5MB,尽管我没有亲自测试过。
IE has a remainingSpace property of the Storage object. The other browsers have no equivalent at this time.
I believe that the default amount of space is 5MB, although I have not tested it personally.
这是一个简单的示例,说明如何执行此操作,并且应该适用于每个浏览器
Here is a simple example of how to do this and should work with every browser
希望这对某人有帮助。
因为 jsfiddle 上的 Jas- 示例对我不起作用,所以我想出了这个解决方案。
(感谢 Serge Seletskyy 和 Shourav 在下面的代码中使用了他们的位)
下面的函数可用于测试 localStorage 有多少可用空间以及(如果 ls 中已有任何键)还剩多少空间。
这有点暴力,但几乎适用于所有浏览器……除了 Firefox。
在桌面 FF 中,它需要很长时间(4-5 分钟)才能完成,而在 Android 上,它只会崩溃。
该函数下面是我在不同平台上的不同浏览器中所做的测试的简短摘要。享受!
正如上面所承诺的,在不同浏览器中进行了一些测试:
GalaxyTab 10.1
iPhone 4s iOS 6.1.3
MacBook Pro OSX 1.8.3 (Core 2 Duo 2.66 8Gb内存)
iPad 3 iOS 6.1.3
Windows 7 -64b (Core 2 Duo 2.93 6Gb 内存)
Win 8(在 Parallels 8 下)
Hope this help someone.
Because Jas- example on jsfiddle does not work for me I came up with this solution.
(thanks to Serge Seletskyy and Shourav for their bits I used in the code below)
Below is the function that can be used to test how much space is available for localStorage and (if any keys are already in lS) how much space is left.
It is a little brute force but it works in almost every browser... apart from Firefox.
Well in desktop FF it takes ages (4-5min) to complete, and on Android it just crashes.
Underneath the function is a short summary of tests that I have done in different browsers on different platforms. Enjoy!
And as promised above some test in different browsers:
GalaxyTab 10.1
iPhone 4s iOS 6.1.3
MacBook Pro OSX 1.8.3 (Core 2 Duo 2.66 8Gb memory)
iPad 3 iOS 6.1.3
Windows 7 -64b (Core 2 Duo 2.93 6Gb memory)
Win 8 (Under Parallels 8)
您可以通过以下方法计算您的本地存储:
最终大小(以字节为单位)将记录在浏览器中。
You can calculate your localstorage by following methods:
Finally size in bytes will be logged in browser.
我解决这个问题的方法是创建函数来查找本地存储中的已用空间和剩余空间,然后创建一个调用这些函数来确定最大存储空间的函数。
The way I went about this problem is to create functions for finding out the used space and remaining space in Local Storage and then a function that calls those functions to determine the max storage space.