Does HTML5 localStorage maximum size include key n

2019-04-06 12:55发布

问题:

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...