9

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?

Audwin Oyong
  • 2,247
  • 3
  • 15
  • 32
Martijn
  • 3,696
  • 2
  • 38
  • 64

2 Answers2

10

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.

Blaster
  • 9,414
  • 1
  • 29
  • 25
  • I'm currently opting for a prefix my application (or rather, jQuery plug-in) name as well. A hash would work only work if repeatable, in which case you might as well just use the name. – Martijn Jul 04 '12 at 18:27
  • @Martijn: Yes I meant a unique hash which does not have chance of collision and encrypt-able and decrypt-able (saving, retrieving) :) That's a different topic though – Blaster Jul 04 '12 at 18:31
6

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

Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
  • So it would be benefitial to use few keys and combine data if possible? – Martijn Jul 04 '12 at 18:23
  • If you really need to maximize space use, yes. But be careful that there are inherent limits in using a non controlled space in a browser that can be replaced or worse (most humans have more than one browser). – Denys Séguret Jul 04 '12 at 18:23
  • 1
    It's not so much about minimizing my own storage footprint, but rather maximizing the space available to other JS scripts. 5MB is quite a lot of space... unless every JS scripts starts treating 5MB as "quite a lot of space". – Martijn Jul 04 '12 at 18:31