I'm using web sql and indexeddb but as a fallback I want to use a btree/localstorage. How much data can I save in localStorage on a given browser/platform?
If no one knows is there a way to determine the size of a javascript object? e.g. JSON.stringify and then multiply by the number of characters? Then I could just write a script that writes to localStorage and reads to see if the value is there and once there is an error or the read stops working that is the magic number.
I need to test this on (ie9, ff, safari, chrome, opera, safari on ipad, androids default browser, dolphin on android, ff on android, opera on android).
If you can help me figure out how to tell the size, in bytes, of a js string then I will run the test and post the results here.
Like SessionStorage, LocalStorage also used for storing the data on the client side. Maximum limit of data saving is about 5 MB in LocalStorage also. LocalStorage has no expiration time, Data in the LocalStorage persist till the user manually delete it.
You can't! The usual limit is 5 MB. Some browsers such as Opera allow you to adjust the size, but this is purely dependent on the browser and is a user-initiated action, not a programmable one. And even if you could remember that localStorage can only store strings, so anything else need to be stringified first.
The global limit is calculated as 50% of free disk space. In Firefox, an internal browser tool called the Quota Manager keeps track of how much disk space each origin is using up, and deletes data if necessary. So if the free space on your hard drive is 500 GB, then the total storage for a browser is 250 GB.
Thanks for the answer allaire. To get an exact answer I ended up writing a script. The ballpark results are that you get a minimum of 5MB on desktop webkit, ff, ie, opera. IE actually let me write 1GB, yes 1 GB of data.
Oddly, ff crashed when trying to write a string with a length of 742 characters but it would write a string of length 1133 characters and this was with the cache cleared so I don't know what's up with that.
This is a messy script but you can run the tests yourself if you want:
<!DOCTYPE />
<html>
<script src="http://code.jquery.com/jquery-1.6.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function () {
var alphabet = "abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ 123456789 {} +-=*/\'[]<>|&^%$#@!,?.";
var i = 0;
var curWord = "";
var tot = 0;
localStorage["sameSpot"] = null;
$("#same").bind("click", function () {
var write = function () {
if (!localStorage["sameSpot"])
localStorage["sameSpot"] = alphabet[i++];
else {
curWord = alphabet[i++] + localStorage["sameSpot"];
localStorage["sameSpot"] = curWord;
}
if (i == alphabet.length)
i = 0;
tot++;
$("#local").html(curWord);
$("#memory").html(localStorage["sameSpot"]);
$("p").html("The number of characters written to localStorage[\"sameSpot\"] is: " + tot);
setTimeout(write, 1);
};
write();
});
var tot2 = 0;
var totChars = 0;
$("#different").bind("click", function () {
var write = function () {
var saveObj = {
alphabet: alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet + alphabet,
date: new Date(),
random: Math.random()
};
saveObj = JSON.stringify(saveObj);
totChars += saveObj.length;
localStorage["t" + tot2] = saveObj;
$("#local").html(saveObj);
$("#memory").html(localStorage["t" + tot2]);
tot2++;
$("p").html("The number of unique entries made in localStorage[0++] is " + tot2 + " and the total number of characters is: " + totChars + " with an average of " + Math.floor(totChars / tot2) + " characters per record");
setTimeout(write, 1);
};
write();
});
});
</script>
<body>
<button id="same">Write Chars To Same Spot</button>
<button id="different">Write Chars To Same Spot</button>
<br />
<p></p>
<textarea rows="50" cols="100" id="memory"></textarea>
<textarea rows="50" cols="100" id="local"></textarea>
</body>
</html>
From Wikipedia:
Storage Size Web storage provides far greater storage capacity (5MB per domain in Mozilla Firefox,[6] Google Chrome, and Opera, 10MB per storage area in Internet Explorer[7]) compared to 4KB (around 1000 times less space) available to cookies.
window.localStorage (from http://msdn.microsoft.com/en-us/library/cc197062(v=vs.85).aspx)
The localStorage attribute provides persistent storage areas for domains. It allows Web applications to store nearly 10 MB of user data, such as entire documents or a user's mailbox, on the client for performance reasons.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With