Created
September 14, 2016 19:02
-
-
Save aradnom/013963b8829ede74012b5ca2b08ad642 to your computer and use it in GitHub Desktop.
How much junk you got in localStorage? Useful for figuring out how close you're getting to the localStorage cap.
This file contains hidden or 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
/** | |
* Return total byte size of all current keys in localStorage. Based on | |
* UTF-8 encoding for byte size, so not guaranteed to be accurate in | |
* other encodings. | |
*/ | |
localStorage.__proto__.size = function () { | |
return Object.keys( this ) | |
.map( function ( key ) { | |
return ( new TextEncoder( 'utf-8' ).encode( localStorage[ key ] ) ).length; | |
}) | |
.reduce( function ( length, next ) { | |
return length + next; | |
}, 0 ); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment