Created
April 1, 2016 14:06
-
-
Save Eccenux/d399e57be8dd884084a1ea78a6984a4c to your computer and use it in GitHub Desktop.
localStorage size check (obviously it only works for current domain)
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
var keys = Object.keys(localStorage); | |
var total = 0; | |
for (var i in keys) { | |
var key = keys[i]; | |
var size = localStorage[key].length; | |
console.log(key, ' size ', getReadableFileSizeString(size)); | |
total += size; | |
} | |
console.log('\n-------\ntotal size ', getReadableFileSizeString(total)); | |
// 100 -> 100 B | |
// 512 -> 0.50 kB | |
function getReadableFileSizeString(fileSizeInBytes) { | |
var i = 0; | |
var byteUnits = [' B', ' kB', ' MB', ' GB', ' TB', 'PB', 'EB', 'ZB', 'YB']; | |
while (fileSizeInBytes > 100) { | |
fileSizeInBytes = fileSizeInBytes / 1024; | |
i++; | |
} | |
return Math.max(fileSizeInBytes, 0.01).toFixed(i > 0 ? 2 : 0) + byteUnits[i]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment