Skip to content

Instantly share code, notes, and snippets.

@aradnom
Created September 14, 2016 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aradnom/013963b8829ede74012b5ca2b08ad642 to your computer and use it in GitHub Desktop.
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.
/**
* 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