如何在Chrome中计算indexedDB表的大小?

7

我正在使用在chrome浏览器上使用pouchDBIndexedDB适配器,我想计算每个IndexedDB数据库的大小。我使用https://github.com/jonnysmith1981/getIndexedDbSize/blob/master/getIndexedDbSize.js中的代码进行计算。

我发现数据库的总大小远大于webkit临时存储的使用量。

下面的屏幕截图是我的应用程序使用的总存储(255MB)。

enter image description here

您将看到有5个存储在IndexedDB中的数据库。以下输出是每个数据库大小的计算结果。您会发现总大小约为389MB。我想知道为什么它们相差如此之大。哪一个是正确的?

--------- _pouch_products -------------
VM1633:51  - attach-seq-store   : 0 B
VM1633:51  - attach-store   : 0 B
VM1633:51  - by-sequence    : 86.7 MB
VM1633:51  - detect-blob-support    : 2 B
VM1633:51  - document-store : 92.3 MB
VM1633:51  - local-store    : 6.1 KB
VM1633:51  - meta-store : 96 B
VM1633:57 TOTAL: 179.0 MB

--------- _pouch_transactions -------------
VM1633:51  - attach-seq-store   : 0 B
VM1633:51  - attach-store   : 0 B
VM1633:51  - by-sequence    : 13.7 KB
VM1633:51  - detect-blob-support    : 2 B
VM1633:51  - document-store : 2.2 KB
VM1633:51  - local-store    : 4.2 KB
VM1633:51  - meta-store : 96 B
VM1633:57 TOTAL: 20.2 KB

--------- _pouch_products-mrview-4c294f20854f412a71c9e7cf2f9cc58f -------------
VM1633:51  - attach-seq-store   : 0 B
VM1633:51  - attach-store   : 0 B
VM1633:51  - by-sequence    : 11.9 MB
VM1633:51  - detect-blob-support    : 0 B
VM1633:51  - document-store : 35.3 MB
VM1633:51  - local-store    : 15.1 MB
VM1633:51  - meta-store : 136 B
VM1633:57 TOTAL: 62.3 MB

--------- _pouch_products-mrview-fdca57d512425c6ed0f20311a4f8d6d1 -------------
VM1633:51  - attach-seq-store   : 0 B
VM1633:51  - attach-store   : 0 B
VM1633:51  - by-sequence    : 86.2 MB
VM1633:51  - detect-blob-support    : 0 B
VM1633:51  - document-store : 44.2 MB
VM1633:51  - local-store    : 17.4 MB
VM1633:51  - meta-store : 136 B
VM1633:57 TOTAL: 147.7 MB
--------- _product_alerts -------------
VM1633:57 TOTAL: 0 B
1个回答

10
Indexed DB API没有提供查询数据库(或store/index)大小的方法。将键和值转换为字节也是由浏览器执行的,脚本无法看到。因此,脚本必须进行近似计算,例如,在将所有键和值序列化为字符串时,计算存储库中所有键和值的大小。
Chrome中的Indexed DB实现使用名为leveldb的后备存储。它具有各种大小优化功能,例如键前缀压缩和使用另一个名为“snappy”的库进行值压缩。字符串也可以以多种方式序列化为字节(例如,JS字符串每个字符占16位,可以朴素地存储为每个字符2个字节或使用UTF-8编码为每个字符1-4个字节)。后备存储还会在删除或覆盖数据时进行惰性压缩,因此它可能暂时占用比需要更多的空间。
这些优化都对脚本不可见,并且所有浏览器都会有所不同,因此近似值将是...近似的。考虑到这一点,389MB与浏览器报告的255MB之间的估计相当不错!
在Chrome中,我们正在尝试通过navigator.storage.estimate() API报告每种存储类型的分组,该API将给出每个存储类型(例如Indexed DB vs. Cache vs. ...)的确切值,尽管它仍然不会给出每个数据库或每个对象存储的值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接