【问题标题】:How to calculate indexedDB table size in chrome?如何在 chrome 中计算 indexedDB 表的大小?
【发布时间】:2019-04-06 04:41:10
【问题描述】:

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

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

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

您会看到IndexedDB 中保存了5 个数据库。下面的输出是每个数据库大小的计算结果。您将看到总大小约为 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

【问题讨论】:

    标签: javascript google-chrome indexeddb pouchdb


    【解决方案1】:

    Indexed DB API 不提供查询数据库(或存储/索引)大小的方法。将键和值转换为字节也是浏览器执行的操作,脚本不可见。所以脚本必须做一个近似,例如当序列化为字符串时,计算存储中所有键和值的大小。

    Chrome 中的 Indexed DB 实现使用名为 leveldb 的后备存储,该存储具有各种大小优化,例如使用另一个名为“snappy”的库进行键前缀压缩和值压缩。字符串也可以以多种方式序列化为字节(例如,JS 字符串每个字符 16 位,可以天真地存储为每个字符 2 个字节或 UTF-8 编码为每个字符 1-4 个字节)。当数据被删除或覆盖时,后备存储也会延迟压缩,因此最终可能会占用比临时需要更多的空间。

    脚本也看不到这些优化,并且所有优化都会因浏览器而异,因此近似值将是……近似值。考虑到所有这些,估计 389MB 与浏览器报告的 255MB 相当不错!

    在 Chrome 中,我们正在试验通过 navigator.storage.estimate() API 报告的每种类型的细分,它会为每种存储类型(例如索引数据库与缓存与...)提供准确的值,尽管它仍然会不提供每个数据库或每个对象的存储值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-12-16
      • 2022-06-16
      • 1970-01-01
      • 2021-02-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多