How to calculate indexedDB table size in chrome?
Asked Answered
M

1

8

I am using pouchDB with IndexedDB adapter on chrome browser and I want to calculate the each IndexedDB database size. I use the code from https://github.com/jonnysmith1981/getIndexedDbSize/blob/master/getIndexedDbSize.js to do the calculation.

What I found is that the total size of the databases is far greater than the webkit temporary storage usage.

Below screenshot is the total storage (255MB) used by my application.

enter image description here

You will see there are 5 databases saved in IndexedDB. And below output is the calculation result for the size of each database. You will see that the total size is about 389MB. I wonder why they are quite different. Which one is the correct one?

--------- _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
Monometallism answered 2/4, 2019 at 10:23 Comment(0)
L
10

The Indexed DB API does not provide a way to query the size of databases (or stores/indexes). Translating keys and values into bytes is also something performed by the browser and is not visible to script. So the script must be doing an approximation, e.g. computing the size of all keys and values in a store when serialized as strings.

The Indexed DB implementation in Chrome uses a backing store called leveldb which has various size optimizations, such as key prefix compression and value compression using another library called "snappy". Strings can also be serialized as bytes in numerous ways (e.g. JS strings are 16-bits per character, which could be naively stored as 2 bytes per character or UTF-8 encoded to 1-4 bytes per character). The backing store also lazily compacts when data is deleted or overwritten, so it may end up taking more space than it needs temporarily.

None of these optimizations are visible to script either, and all will vary cross browsers, so the approximation will be... approximate. Given all of this, an estimate of 389MB vs. the 255MB the browser reports is pretty good!

In Chrome we're experimenting with a per-type breakdown reported via the navigator.storage.estimate() API that would give the exact value for each storage type (e.g. Indexed DB vs. Cache vs. ...), although it still would not give per-database or per-object store values.

Liquorish answered 5/4, 2019 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.