Easypeasy
Easypeasy

Reputation: 160

Unexpected bytes used in chrome storage

I'm writing an extension that makes use of the chrome.storage API. I want to truncate each item to make sure it is below the maximum bytes threshold of the storage (local and sync).

The documentation states that the byte size of each individual item is

measured by the JSON stringification of its value plus its key length.

I use the following code to calculate the expected byte size:

new TextEncoder().encode(JSON.stringify(value)).length + key.length

I use the following code to check actual usage:

chrome.storage.<storage-area>.set({ [key]: value }, () => {
   chrome.storage.<storage-area>.getBytesInUse(key, bytes => {
        console.log("actual bytes in use", bytes);
   });
});

Given a key of "test" and a value of "abc", the expected byte usage is 9b. The actual byte usage is 9b.

Given a key of "test" and a value of "«ταБЬℓσ»", the expected byte usage is 23b. The actual byte usage is 23b.

Given a key of "test" and a value of "<", the expected byte usage is 7b. The actual byte usage is 12b.

The storage is of course cleared between each check.

In the last example, what is causing those 5 extra, unexpected, bytes? What am I missing?

Edit: I'm using Google Chrome version 73.0.3683.75 (Official Build) (64-bit)

Upvotes: 3

Views: 232

Answers (1)

Easypeasy
Easypeasy

Reputation: 160

I found the reason thanks to w0xx0m's comment.

Chrome/Chromium replaces the less than character with "\u003C" to prevent script execution.

Source code can be found here.

Upvotes: 2

Related Questions