Sometimes, we want to find the size of localStorage with JavaScript.
In this article, we’ll look at how to find the size of localStorage with JavaScript.
How to find the size of localStorage with JavaScript?
To find the size of localStorage with JavaScript, we can convert it to a blob and get its size.
For instance, we write
const size = new Blob(Object.values(localStorage)).size;
to convert the localStorage
object to a blob with
Blob(Object.values(localStorage))
Then we get the size of the blob with the size
property.
Conclusion
To find the size of localStorage with JavaScript, we can convert it to a blob and get its size.
One reply on “How to find the size of localStorage with JavaScript?”
Hi. Using this method, are you then creating a new object which takes up the same amount of memory as the localStorage content? It seems a high-burden method to me to have to copy it all, especially if you’re going to be doing it most in circumstances where you’re concerned about filling the store to capacity.
Perhaps this would be a candidate for a new API property or method like localStorage.getSize(). The browser should surely be able to attain a result far more efficiently. localStorage.getCapacity() while we’re at it too.