Calculate QUOTA_BYTES_PER_ITEM

388 views
Skip to first unread message

Simeon Velichkov

unread,
Mar 8, 2024, 11:46:21 AMMar 8
to Chromium Extensions
Does anyone of you knows how to properly calculate the QUOTA_BYTES_PER_ITEM?

I'm having something like this at the root of my sync storage:

{something:{another:'very large string'}}

so I figured that I need to do:

JSON.stringify({something:{another:'very large string'}}).length

but in my current case it allows me to store up to 8151 bytes instead of 8192

So I'm wondering what am I doing wrong

Patrick Kettner

unread,
Mar 8, 2024, 11:55:31 AMMar 8
to Simeon Velichkov, Chromium Extensions
Hi Simeon!
Are you accounting for the bytes used by the braces and JSON keys?

--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/1173296a-77ce-4a26-affd-f5ee9422b740n%40chromium.org.

Simeon Velichkov

unread,
Mar 8, 2024, 12:02:16 PMMar 8
to Chromium Extensions, Patrick Kettner, Chromium Extensions, Simeon Velichkov
I think so, that's why I'm using JSON.stringify. My goal is to display an error without having to try to insert it, catch the error, and only then present it to the user.

Now with JSON.stringify('extension-id':{{something:{another:'very large string'}}}).length I got it up to 8188 so 4 bytes still missing, but I don't know if I am on the right path or not.

Juraj M.

unread,
Mar 8, 2024, 12:33:50 PMMar 8
to Chromium Extensions, Simeon Velichkov, Patrick Kettner, Chromium Extensions
There is a bug that causes "<" character to occupy more than it should:

Simeon Velichkov

unread,
Mar 8, 2024, 1:20:29 PMMar 8
to Chromium Extensions, Juraj M., Simeon Velichkov, Patrick Kettner, Chromium Extensions
I don't have that character in my string, but I have to admit that this revelations doesn't make the situation any better.

wOxxOm

unread,
Mar 9, 2024, 10:05:46 AMMar 9
to Chromium Extensions, Simeon Velichkov, Juraj M., Patrick Kettner, Chromium Extensions
  1. chrome.storage.sync item quota also includes the sync server meta, which is IIRC about 40 bytes minimum, it was mentioned somewhere in crbug. I don't remember if it may grow beyond that.
  2. Judging by the source code Chrome converts the result to UTF8, which may significantly increase it. You can do the same using new TextEncoder().encode(JSON.stringify(data)).length

med ism

unread,
Jun 4, 2024, 1:39:09 PMJun 4
to Chromium Extensions, wOxxOm, Simeon Velichkov, Juraj M., Patrick Kettner, Chromium Extensions
hello , use something like this , to calculate bytes per item 'Key' before sync into chrome storage to avoid the error of 8192 bytes exceeded
chrome.storage.sync.getBytesInUse(key, function(bytesInUse) {
        if (bytesInUse > chrome.storage.sync.QUOTA_BYTES_PER_ITEM) {
            console.log('error QUOTA_BYTES_PER_ITEM '+bytesInUse);
}})

woxxom

unread,
Jun 4, 2024, 4:22:04 PMJun 4
to Chromium Extensions, med ism, wOxxOm, Simeon Velichkov, Juraj M., Patrick Kettner, Chromium Extensions
getBytesInUse won't help because it returns the overall usage for all items, while QUOTA_BYTES_PER_ITEM is about a single item.

med ism

unread,
Jun 11, 2024, 9:33:49 PMJun 11
to Chromium Extensions, woxxom, med ism, Simeon Velichkov, Juraj M., Patrick Kettner, Chromium Extensions
 nope it can be used for single item, in my exemple it was used for item(key), here is the doc about it 
https://developer.chrome.com/docs/extensions/reference/api/storage
Screenshot 2024-06-12 023146.png

woxxom

unread,
Jun 12, 2024, 1:44:34 AMJun 12
to Chromium Extensions, med ism, woxxom, Simeon Velichkov, Juraj M., Patrick Kettner, Chromium Extensions
I was blind and didn't see "key" in the parameters, thanks.

Simeon Velichkov

unread,
Jun 12, 2024, 10:13:51 AMJun 12
to Chromium Extensions, woxxom, med ism, Simeon Velichkov, Juraj M., Patrick Kettner, Chromium Extensions
Isn't our code supposed to be 

bytesInUse + theThingYouWantToInsert > chrome.storage.sync.QUOTA_BYTES_PER_ITEM

Also the question still remains on how to calculate the theThingYouWantToInsert correctly, or maybe the getBytesInUse call would take into account the metadata bytes for that key? I did not try it tbh.

And the original question was about how to get the bytes in use for a nested key, which for the storage may it doesn't matter, but I have nested objects under each key. Because at any given time any of the nested keys can have any value and using the getBytesInUse won't give me the answer of how much bytes do I have left for the key that I am interested in particular.
Reply all
Reply to author
Forward
0 new messages