Spero che questo aiuti qualcuno.
Poiché Jas- esempio su jsfiddle non funziona per me, ho trovato questa soluzione. (grazie a Serge Seletskyy e Shourav per i loro bit che ho usato nel codice qui sotto)
Di seguito è riportata la funzione che può essere utilizzata per verificare quanto spazio è disponibile per localStorage e (se qualche chiave è già in lS) quanto spazio è rimasto.
È un po 'di forza bruta ma funziona in quasi tutti i browser ... a parte Firefox. Ebbene, nel desktop FF ci vogliono anni (4-5 minuti) per completare, e su Android si blocca.
Sotto la funzione c'è un breve riassunto dei test che ho fatto in diversi browser su diverse piattaforme. Godere!
function testLocalStorage() {
var timeStart = Date.now();
var timeEnd, countKey, countValue, amountLeft, itemLength;
var occupied = leftCount = 3; //Shurav's comment on initial overhead
//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;
}
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);
//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);
//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
}
E come promesso sopra alcuni test in diversi browser:
GalaxyTab 10.1
- Maxthon Pad 1.7 ~ 1130ms 5Mb
- Firefox 20.0 (Beta 20.0) si è bloccato entrambi
- Chrome 25.0.1364.169 ~ 22250ms / 5Mb
- Nativo (identificato come Safari 4.0 / Webkit534.30) ~ 995ms / 5Mb
iPhone 4s iOS 6.1.3
- Safari ~ 520 ms / 5 Mb
- Come HomeApp ~ 525 ms / 5 Mb
- iCab ~ 710 ms / 5 MB
MacBook Pro OSX 1.8.3 (Core 2 Duo 2.66 8 Gb di memoria)
- Safari 6.0.3 ~ 105 ms / 5 Mb
- Chrome 26.0.1410.43 ~ 3400ms / 5Mb
- Firefox 20.0 300150ms (!) / 10Mb (dopo essersi lamentati di uno script troppo lungo)
iPad 3 iOS 6.1.3
- Safari ~ 430 ms / 5 Mb
- iCab ~ 595 ms / 5 MB
Windows 7-64b (Core 2 Duo 2.93 6 Gb di memoria)
- Safari 5.1.7 ~ 80 ms / 5 Mb
- Chrome 26.0.1410.43 ~ 1220 ms / 5 Mb
- Firefox 20.0 228500ms (!) / 10Mb (dopo essersi lamentati del fatto che lo script fosse troppo lungo)
- IE9 ~ 17900ms /9.54Mb (se nel codice sono presenti console.log non funziona fino all'apertura di DevTools)
- Opera 12.15 ~ 4212ms /3.55Mb (questo è quando 5Mb è selezionato, ma Opera chiede gentilmente se vogliamo aumentare la quantità di lS, sfortunatamente si blocca se il test viene eseguito più volte di seguito)
Vinci 8 (sotto Parallels 8)