如何以编程方式获取 indexedDB 中每个 objectStore 的当前数据大小?

How to get current data size of each objectStore in indexedDB programmatically?

我想以编程方式获取 indexedDB 中每个 objectStore 的当前数据大小(MB/GB 中的用法)。

我认为这可能是一个热门问题,但我在 Whosebug 中找不到任何类似的问题questions/answers。

这可能吗?

如果数据库条目的数量很多,这段代码会花费很多时间,但我可以通过这段代码得到结果。

    const countDB = async (db, table) => {
        return new Promise((resolve, reject) => {
            const tx = db.transaction([table], 'readonly');
            const store = tx.objectStore(table);
            const cursorReq = store.openCursor();
            let count = 0;
            let size = 0;
            cursorReq.onsuccess = function(e) {
                const cursor = cursorReq.result;
                if (cursor) {
                    count++;
                    size = size + cursor.value.blob.size;
                    cursor.continue();
                }
            };
            cursorReq.onerror = function(e) {
                reject(e);
            };
            tx.oncomplete = function(e) {
                resolve({
                    count: count,
                    size: size
                });
            };
            tx.onabort = function(e) {
                reject(e);
            };
            tx.onerror = function(e) {
                reject(e);
            };
        });
    };