Last active
December 15, 2015 19:29
-
-
Save jakubgg/5311678 to your computer and use it in GitHub Desktop.
JavaScript function to test (a bit brute force test) how much space is allocated for localStorage in a browser, if there are already entries in the localStorage it will show how much space is left.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Function to test how much space is allocated for localStorage in a browser, | |
* if there are already entries in the localStorage it will show how much space is left. | |
* console.logs commented out because IE is tripping over them. | |
*/ | |
function testLocalStorage(){ | |
var timeStart = Date.now(); | |
var timeEnd, countKey, countValue, amountLeft, itemLength; | |
var occupied = leftCount = 0; | |
//create localStorage entries until localStorage is totally filled and browser issues a warning. | |
var i=0; | |
while (!error){ | |
try { | |
//length of the 'value' was picked to be a compromise between speed and accuracy, | |
// the longer the 'value' the quicker script and result less accurate. This one is around 2Kb | |
localStorage.setItem('testKey'+i, '11111111112222222222333333333344444444445555555555666661111111111222222222233333333334444444444555555555566666'); | |
} catch (e) { | |
var error = e; | |
//console.log('error: '+ e); | |
} | |
i++; | |
} | |
//if the warning was issued - localStorage is full. | |
if (error){ | |
//iterate through all keys and values to count their length | |
for (var i = 0; i < localStorage.length; i++){ | |
countKey = localStorage.key(i); | |
countValue = localStorage.getItem(localStorage.key(i)); | |
itemLength = countKey.length + countValue.length; | |
//if the key is one of our 'test' keys count it separately | |
if(countKey.indexOf("testKey") !== -1){ | |
leftCount = leftCount + itemLength; | |
} | |
//count all keys and their values | |
occupied = occupied + itemLength; | |
}; | |
//all keys + values lenght recalculated to Mb | |
occupied = (((occupied*16)/(8*1024))/1024).toFixed(2); | |
//console.log("total localStorage: "+occupied+"Mb"); | |
//if there are any other keys then our 'testKeys' it will show how much localStorage is left | |
amountLeft = occupied - (((leftCount*16)/(8*1024))/1024).toFixed(2); | |
//console.log("localStorage left: "+amountLeft+"Mb"); | |
//iterate through all localStorage keys and remove 'testKeys' | |
Object.keys(localStorage).forEach(function(key){ | |
if (key.indexOf("testKey") !== -1) { | |
localStorage.removeItem(key); | |
} | |
}); | |
} | |
//calculate execution time | |
var timeEnd = Date.now(); | |
var time = timeEnd - timeStart; | |
//create message | |
var message = 'Finished in: ' + time + 'ms \n total localStorage: '+occupied+'Mb \n localStorage left: '+amountLeft+"Mb"; | |
//put the message on the screen | |
document.getElementById('scene').innerText = message; //this works with Chrome,Safari, Opera, IE | |
//document.getElementById('scene').textContent = message; //Required for Firefox to show messages | |
//console.log(message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Tests of testLocalStorage in different browsers on different platforms. | |
GalaxyTab 10.1 | |
Maxthon Pad 1.7 ~1130ms 5Mb | |
Firefox 20.0(Beta 20.0) crashed both | |
Chrome 25.0.1364.169 ~22250ms /5Mb | |
Native (identifies as Safari 4.0/Webkit534.30) ~995ms /5Mb | |
iPhone 4s iOS 6.1.3 | |
Safari ~ 520ms /5Mb | |
As HomeApp ~525ms / 5Mb | |
iCab ~ 710ms /5mb | |
MacBook Pro OSX 1.8.3 (Core 2 Duo 2.66 8Gb memory) | |
Safari 6.0.3 ~105ms /5Mb | |
Chrome 26.0.1410.43 ~3400ms /5Mb | |
Firefox 20.0 300150ms(!) /10Mb (after complaining about script running to long) | |
iPad 3 iOS 6.1.3 | |
Safari ~430ms /5Mb | |
iCab ~595ms /5mb | |
Windows 7 -64b (Core 2 Duo 2.93 6Gb memory) | |
Safari 5.1.7 ~80ms /5Mb | |
Chrome 26.0.1410.43 ~1220ms /5Mb | |
Firefox 20.0 228500ms(!) /10Mb (after complaining about script running to long) | |
IE9 ~17900ms /9.54Mb ( if any console.logs are in the code does not work until DevTools are opened) | |
Opera 12.15 ~4212ms /3.55Mb (this is when 5Mb is selected, but Opera asks nicely if we want increase the amount of lS, unfortunately it crashes if test conducted a few times in a row) | |
Win 8 (Under Parallels 8) | |
IE10 ~7850ms /9.54Mb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment