Does HTML5 localStorage maximum size include key names?

匿名 (未验证) 提交于 2019-12-03 03:06:01

问题:

HTML5's localStorage WebStorage has a maximum size of 5MB.

Does this include the key names?

For instance, if I were to use key names "quite-a-long-key-name-and-this-is-only-1" instead of "key1", would I run out of space sooner?

On a slightly related topic; is there any de-facto convention for naming localStorage keys? How are namespace collisions prevented when using third party JS scripts?

回答1:

Does this include the key names?

Yes those do, they become part of data eg they identify data you store and later retrieve so that got to be saved as well.

How are namespace collisions prevented when using third party JS scripts?

That's a good question, I generally prefix localStorage with application name. Though a better approach would be to create a hash eg some algorithim that accepts a string like application name, etc and later when reading you use them again.



回答2:

First note that this is implementation dependent as the norm doesn't give a limit. So you shouldn't rely on the size.

Secondly, yes, the limit in today's browsers includes the names : this is the size of the storage space ("disk space").

In order to avoid collisions, I use namespace (eg myplugin.mypart.myval). 5 MB is already big for a storage that can be deleted or not available any more at any time so I never thought about reducing the size of the keys...



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!