Skip to content

Instantly share code, notes, and snippets.

@tralves
Last active March 9, 2024 13:34
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save tralves/9e5de2bd9f582007a52708d7d4209865 to your computer and use it in GitHub Desktop.
Save tralves/9e5de2bd9f582007a52708d7d4209865 to your computer and use it in GitHub Desktop.
Calculate sizes of all IndexDB database and tables
var getTableSize = function(db, dbName){
return new Promise((resolve,reject) => {
if (db == null) {
return reject();
}
var size = 0;
db = event.target.result;
var transaction = db.transaction([dbName])
.objectStore(dbName)
.openCursor();
transaction.onsuccess = function(event){
var cursor = event.target.result;
if(cursor){
var storedObject = cursor.value;
var json = JSON.stringify(storedObject);
size += json.length;
cursor.continue();
}
else{
resolve(size);
}
}.bind(this);
transaction.onerror = function(err){
reject("error in " + dbName + ": " + err);
}
});
};
var getDatabaseSize = function (dbName) {
var request = indexedDB.open(dbName);
var db;
var dbSize = 0;
request.onerror = function(event) {
alert("Why didn't you allow my web app to use IndexedDB?!");
};
request.onsuccess = function(event) {
db = event.target.result;
var tableNames = [ ...db.objectStoreNames ];
(function(tableNames, db) {
var tableSizeGetters = tableNames
.reduce( (acc, tableName) => {
acc.push( getTableSize(db, tableName) );
return acc;
}, []);
Promise.all(tableSizeGetters)
.then(sizes => {
console.log('--------- ' + db.name + ' -------------');
tableNames.forEach( (tableName,i) => {
console.log(" - " + tableName + "\t: " + humanReadableSize(sizes[i]));
});
var total = sizes.reduce(function(acc, val) {
return acc + val;
}, 0);
console.log("TOTAL: " + humanReadableSize(total))
});
})(tableNames, db);
};
};
var humanReadableSize = function (bytes) {
var thresh = 1024;
if(Math.abs(bytes) < thresh) {
return bytes + ' B';
}
var units = ['KB','MB','GB','TB','PB','EB','ZB','YB'];
var u = -1;
do {
bytes /= thresh;
++u;
} while(Math.abs(bytes) >= thresh && u < units.length - 1);
return bytes.toFixed(1)+' '+units[u];
}
var printIndexDBSizes = function() {
indexedDB.webkitGetDatabaseNames().onsuccess = function (e) {
var databaseNames = e.target.result;
var dbName;
for(var i=0; i < databaseNames.length; i++) {
dbName = databaseNames.item(i);
getDatabaseSize(dbName);
};
};
}
//usage
printIndexDBSizes();
@trialforce
Copy link

Don't work on current chrome.

@danmana
Copy link

danmana commented Jan 31, 2019

Chrome no longer supports using indexedDB.webkitGetDatabaseNames to get all databases.

You will have to specify them by hand.

var printIndexDBSizes = function() {
    var databaseNames = ['myDatabaseName'];
    var dbName;
    for(var i=0; i < databaseNames.length; i++) {
      dbName = databaseNames[i];
      getDatabaseSize(dbName);
    };
}

@danmana
Copy link

danmana commented Jan 31, 2019

If you also store Blob objects in the database you need to count those separately (JSON.stringify prints Blobs as {}).

For example this adds the size of all direct Blob properties of each object in the db (doesn't go recursively in the json structure):

Replace line 17 with

  size += json.length;

  var key;
  for (key in storedObject) {
    if (storedObject.hasOwnProperty(key) && storedObject[key] instanceof Blob) {
      size += storedObject[key].size;
    }
  }

@zeyios
Copy link

zeyios commented Apr 6, 2023

We can use indexedDB.databases(); to get all databases

@crispart
Copy link

crispart commented Dec 30, 2023

For better non-latin symbols support replace line 17 with size += new Blob([json]).size

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment