Skip to content

Instantly share code, notes, and snippets.

@tkambler
Last active August 2, 2024 09:53
Show Gist options
  • Save tkambler/71050d80f1a57ea83c18 to your computer and use it in GitHub Desktop.
Save tkambler/71050d80f1a57ea83c18 to your computer and use it in GitHub Desktop.
Calculate size of used LocalStorage space
/**
* Returns the total amount of disk space used (in MB) by localStorage for the current domain.
*/
var getLocalStorageSize = function() {
var total = 0;
for (var x in localStorage) {
// Value is multiplied by 2 due to data being stored in `utf-16` format, which requires twice the space.
var amount = (localStorage[x].length * 2) / 1024 / 1024;
total += amount;
}
return total.toFixed(2);
};
@yllieth
Copy link

yllieth commented May 12, 2020

I had 2 little bugs:

  • I encountered a NaN amount
  • for ... in also takes stuff in the prototype of localStorage object (like getItem function, length property, ... but the impact is really slow)

I had to add a little condition to fix that:

var getLocalStorageSize = function() {
    var total = 0;
    for (var x in localStorage) {
        // Value is multiplied by 2 due to data being stored in `utf-16` format, which requires twice the space.
        var amount = (localStorage[x].length * 2) / 1024 / 1024;
        if (!isNaN(amount) && localStorage.hasOwnProperty(x)) {
            // console.log(x, localStorage.getItem(x), amount);
            total += amount;
        }
    }
    return total.toFixed(2);
};

but thank you very much for this ❤️.

@ewwink
Copy link

ewwink commented Jul 16, 2020

thanks

@medamine980
Copy link

the capacity of local storage in Chrome is up to 10Mb right?

@kissu
Copy link

kissu commented Aug 3, 2021

Not working for me > "0.00".

This one does work well tho: https://stackoverflow.com/a/15720835/8816585

var _lsTotal=0,_xLen,_x;for(_x in localStorage){ if(!localStorage.hasOwnProperty(_x)){continue;} _xLen= ((localStorage[_x].length + _x.length)* 2);_lsTotal+=_xLen; console.log(_x.substr(0,50)+" = "+ (_xLen/1024).toFixed(2)+" KB")};console.log("Total = " + (_lsTotal / 1024).toFixed(2) + " KB");

@bethropolis
Copy link

The above was great but I needed it as a function and I only need the Total size.

This one seems better

let localStorageSize = function () {
   let _lsTotal = 0,_xLen, _x;
   for (_x in localStorage) {
   if (!localStorage.hasOwnProperty(_x)) continue;
       _xLen = (localStorage[_x].length + _x.length) * 2;
       _lsTotal += _xLen;
   }
 return  (_lsTotal / 1024).toFixed(2);
}

you can use it like this

console.log( `size: ${localStorageSize()}kb`)

response

size: 3.63kb

thanks ❤

@amineshon
Copy link

function calculateTotalLocalStorageUsage() {
let total = 0;
for (let key in localStorage) {
let value = localStorage.getItem(key);
total += (new TextEncoder().encode(value)).length;
}
let inKB = (total / 1024);
return inKB.toFixed(2);
}

// Log the total localStorage usage
console.log("Total localStorage usage: " + calculateTotalLocalStorageUsage() + " kb");

@Kolostov
Copy link

Hi, came across this discussion and decided to add my input

Object.keys(localStorage).map(k => (localStorage.getItem(k) ?? '').length * 2 / 1024 / 1024).reduce((a, b) => a + b);

a one-liner

@patik
Copy link

patik commented Aug 2, 2024

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment