user1365010
user1365010

Reputation: 3343

Illogical size of localstorage

I read that the max size of the localStorage is around 5MB. But I did something very strange that shows another result.


I copied a 1024x768 image from internet (for startup image) then converted it to base 64. I put it in a file and saw that the file was 300KB. Then, I wrote the code below :

for(var a;a<1000;a++)
{
    try{localStorage["'"+a+"'"]="The very long string of 75000 characteres";}
    catch(e)
    {
        alert("No !");
        break;
    }
}
alert("Yes !");

And my iPad alerted Yes !. 1000*300=300000KB -> 300MB. It means that the localStorage stored 300MB of information!

How is this possible? Is the localStorage compressing or something?

EDIT : Finally, it was a typo. for(var a;a<1000;a++) instead of for(var a=0;a<1000;a++). And if someone wants to know, I storred succesfully 6 images.

Upvotes: 3

Views: 101

Answers (1)

Leopd
Leopd

Reputation: 42747

Your for loop never executed because you didn't initialize your loop variable. Try

for(var a=0;a<1000;a++)

Upvotes: 4

Related Questions