9

Lets say i create a localstorage key and give it an empty string. Does the name of the keyitem take up the same amount of space as the value would per character?

for instance does

localStorage.setItem("keyitem","") 
//Equal the space of this other one under?
localStorage.setItem("key","item");

Also, does the amount of keys matter? for instance

localStorage.setItem("key","");
//Equal the amount of storage as the 3 under combined? 
localStorage.setItem("k","");
localStorage.setItem("o","");
localStorage.setItem("h","");
Razvan Dumitru
  • 11,815
  • 5
  • 34
  • 54
Mattias
  • 2,929
  • 5
  • 29
  • 46
  • The amount of keys is pretty much binary - `0` or `1`. You cannot have multiple keys with the same name, multiple calls to `setItem` just overwrite the previous value. – Bergi Apr 29 '15 at 13:19
  • 5
    This is browser specific as each browser stores their data differently, ie chrome/firefox I believe use sqlite dbs, opera xml files – Patrick Evans Apr 29 '15 at 13:20
  • Keep in mind that this is probably just what the browser want's to do with it. I guess it would store 16 or 32 bit strings as both key and value. But I'm not sure. (What the browser wants to do with it being: They implement it differently, probs) – Mathijs Segers Apr 29 '15 at 13:20
  • I edited to different keyitems – Mattias Apr 29 '15 at 13:20

5 Answers5

1

Does the name of the keyitem take up the same amount of space as the value would per character?

No, it is not necessary. The amount of space taken by key could be more than the amount of space taken by value. But together the space taken by key and value should be approx 5MB(although this differs with browser as it is browser dependent)

You can use this code to test:

localStorage.clear();
localStorage.setItem(new Array(5e6).join(' '),'');
localStorage.key(0).length;

Output on Chrome for the above test:

enter image description here

So as long as it comes under 5MB(which is mostly the upper limit for most browsers) your key can have any length

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • Also to mention that 5MB is equal to 2.5 Million characters on Chrome. Also this: http://stackoverflow.com/questions/11333807/does-html5-localstorage-maximum-size-include-key-names/11333871#11333871 – Rahul Tripathi Apr 29 '15 at 13:43
  • @Rahil Tripathi Would u in that case say that 5MB also is equal to 2.5 Million localStorage.setItem("h",""); which also only contain one character? – Mattias Apr 29 '15 at 13:48
  • @mattias:- No, that is the upper limit(*that too is specific to browsers*). You can run the test which I have added in your browser(*In firefox is making approx 2.94MB for me*) and you will get the space which your key is taking. In case when you are using key as 'k' then it is not 5MB. Does that make sense now? – Rahul Tripathi Apr 29 '15 at 13:50
  • @mattias:- So if you want to check like `localStorage.setItem("kgfdshfgsdhfdksfd.........2.5million characters","");` You will get that the space is coming close to 5MB atleast in Chrome – Rahul Tripathi Apr 29 '15 at 13:52
  • Okay well that answers that the key takes equal space as the value itself i suppose, cant see the test u added? which link is it supposed to be? ye it's different upper values but what i meant was, if there is 2.5 million different localstorage keys with only one character as the keyname, would that equal one key with a keyname of 2.5 million characers? – Mattias Apr 29 '15 at 14:03
  • @mattias:- You can test it on your browser console. – Rahul Tripathi Apr 29 '15 at 14:05
  • @mattias:- For the second part of your answer, I would guess that it would be equivalent. – Rahul Tripathi Apr 29 '15 at 14:07
  • @mattias:- Added the console result for my test. Also if you want you can increase `5e6` to `5e7` and you will see the difference! :) – Rahul Tripathi Apr 29 '15 at 14:13
1

I found a function once to calculate the size of the localStorage and sessionStorage objects, but I can't remember where I found it.

Here's the code:

Storage.prototype.size = function(units) {
    'use strict';
    units = units ? units.toUpperCase() : 'MB';
    var size = unescape(encodeURIComponent(JSON.stringify(this))).length;
    switch (units) {
        case 'B': return [size,'B'].join(' ');
        case 'KB': return [+(size / 1024).toFixed(3),'KB'].join(' ');
        default: return [+(size / 1024 / 1024).toFixed(3),'MB'].join(' ');
    }
};

I decided to go ahead and run some tests in various browsers.

Firefox (37.0.2):

Firefox console output

Chrome (42.0.2311.90 m):

Chrome console output

IE 11 (11.0.9600.17420):

IE Console Output

Opera (29.0.1795.47):

Opera console output

So it looks like FireFox, Chrome, and Opera (probably Safari as well, but I don't have that) all have the same behavior, and keys take up far more space than their values.

In IE (good old IE...), the implementation is executed in a way such that it doesn't matter how you store something.

Community
  • 1
  • 1
RevanProdigalKnight
  • 1,316
  • 1
  • 14
  • 23
  • 2
    I'll quote a comment on a later answer: "This does not show how much space the actual key takes up. This just shows the amount of characters a JSON representation of localStorage has. Each browser implements its own way of saving data. Each of those methods may store the data completely differently than the other. For instance one may use utf8 encoding while the other may use utf16 (meaning the use of 2 bytes to represent 1 character), some may use compression, etc. – Patrick Evans". This applies to your method as well -- you just measure the JSON string. (ctd) – Scimonster Apr 29 '15 at 15:21
  • (ctd) And the IE implementation doesn't really look different from here. The problem is with the key `key` -- in most implementations, that isn't included the the JSON representation. Apparently IE does include it in this case. Therefore, you're getting the JSON `{"keyitem":""}` which is 14 bytes for the first, and `{}` for the second. In IE, it's `{"key":"item"}` for the second. So you can't really tell from here. -1 – Scimonster Apr 29 '15 at 15:23
  • @Scimonster And I will quote a response to the comment you quoted: "I agree, but is the best proxy to understand what gets stored in the localStorage. - Jaco de Groot" – RevanProdigalKnight Apr 29 '15 at 15:27
0

You can determine this yourself by using the JSON stringify methods to turn the localStorage object into a JSON Object:

JSON.stringify(localStorage).length

This will not return the actual size of the key as data can be stored as utf16 (2 bytes) or utf8 (1 byte), but it is a good proxy for the actual size. See a code snippet below:

 localStorage.clear()
 localStorage.setItem("keyitem","") 
 document.write(JSON.stringify(localStorage).length+'</br>')

 //Equal the space of this other one under?
 localStorage.setItem("key","item");
 document.write(JSON.stringify(localStorage).length+'</br>')

 localStorage.clear();
 localStorage.setItem("key","");
 document.write(JSON.stringify(localStorage).length+'</br>')


 //Equal the amount of storage as the 3 under combined? 
 localStorage.setItem("k","");
 localStorage.setItem("o","");
 localStorage.setItem("h","");
 document.write(JSON.stringify(localStorage).length+'</br>')
Alex
  • 21,273
  • 10
  • 61
  • 73
  • So you're saying all browsers store it as a JSON string? I guess this makes sense as it is Javascript Object Notation, but how'd you know? I am of interest – Mathijs Segers Apr 29 '15 at 13:25
  • 2
    What's your answer? I have no idea what you determined in your jsFiddle, and jsFiddle isn't loading for me right now. This is why we put all the important details directly here instead of on some other crappy site. –  Apr 29 '15 at 13:27
  • My apologies, this code snippet doesn't work on StackOverflow itself. – Alex Apr 29 '15 at 13:37
  • 4
    This does not show how much space the actual key takes up. This just shows the amount of characters a JSON representation of localStorage has. Each browser implements its own way of saving data. Each of those methods may store the data completely differently than the other. For instance one may use utf8 encoding while the other may use utf16 (meaning the use of 2 bytes to represent 1 character), some may use compression, etc. – Patrick Evans Apr 29 '15 at 13:38
  • I agree, but is the best proxy to understand what gets stored in the localStorage. – Alex Apr 29 '15 at 13:51
0

I would say that this is an implementation detail for what is concerning the real structure of the storage on disk.

In practice you are given a certain amount of space per origin (usually 5MiB, to check the actual storage you can use this test tool linked in the MDN documentations) and you can store data (both in terms of keys and values) as long as you don't exceed that size, as shown in a previous answer. So yes the keys are included in the storage quota.

As it points out in the test tool I included, chars are actually UTF-16 so they takes up 2 bytes of your storage quota.

Also note that the storage stores strings, this means that if you put a large float as key or value you are not storing it in its binary format but as strings!

// These takes up the same space
localStorage.setItem("a", 123);
localStorage.setItem("a", "123");

In fact if you try to do typeof of the following you get string in both cases

localStorage.setItem("123", "");
localStorage.setItem(123, "");
typeof localStorage.key(0); // returns "string"
typeof localStorage.key(1); // returns "string" as well

As for the second part, in terms of storage this

localStorage.setItem("k","");
localStorage.setItem("o","");
localStorage.setItem("h","");

should take 3 chars of storage from your quota, that is 3 * 2 bytes if using UTF-16 or 3 bytes if using UTF-8.

For an overview of local storage solutions you can check here http://www.html5rocks.com/en/features/storage

Community
  • 1
  • 1
qwattash
  • 855
  • 7
  • 14
0

I ran a test in Chrome and was able to prove that the key takes up exactly as much space as the length.

With a key of abc, i was able to set a value of length 5242877. With a key of a, i was able to set a value of length 5242879. As you can see, removing characters from the key freed up those 2 for the value.

Scimonster
  • 32,893
  • 9
  • 77
  • 89
  • okay great! what about " if there is 2.5 million(for instance) different localstorage keys with only one character as the keyname, would that equal one key with a keyname of 2.5 million characers in space?" – Mattias Apr 29 '15 at 16:54
  • Yeah, it seems like it. – Scimonster Apr 29 '15 at 17:08