Skip to content

Instantly share code, notes, and snippets.

@yesvods
Created January 21, 2016 09:54
Show Gist options
  • Save yesvods/94e2d37777ba6bb1d8a1 to your computer and use it in GitHub Desktop.
Save yesvods/94e2d37777ba6bb1d8a1 to your computer and use it in GitHub Desktop.
LocalStorage MaxSzie
if (localStorage && !localStorage.getItem('size')) {
var i = 0;
try {
// Test up to 10 MB
for (i = 250; i <= 10000; i += 250) {
localStorage.setItem('test', new Array((i * 1024) + 1).join('a'));
}
} catch (e) {
localStorage.removeItem('test');
localStorage.setItem('size', i - 250);
}
}
function roughSizeOfObject( object ) {
var objectList = [];
var stack = [ object ];
var bytes = 0;
while ( stack.length ) {
var value = stack.pop();
if ( typeof value === 'boolean' ) {
bytes += 4;
}
else if ( typeof value === 'string' ) {
bytes += value.length * 2;
}
else if ( typeof value === 'number' ) {
bytes += 8;
}
else if
(
typeof value === 'object'
&& objectList.indexOf( value ) === -1
)
{
objectList.push( value );
for( var i in value ) {
stack.push( value[ i ] );
}
}
}
return bytes;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment