About chrome.storage

180 views
Skip to first unread message

c luo

unread,
Oct 22, 2022, 2:40:47 AM10/22/22
to Chromium Extensions
Should I storge data in chrome.storage flat or structured?Can flat way be faster and more convenient than the structured way? It seems harder when update date using structured way.

Flat way:
chrome.storage.local.set({
    a1: "xxx",
    a2: "xxx",
    b1: "xxx",
    b2: "xxx"
});

Structured way:
chrome.storage.local.set({
a :{
    a1: "xxx",
    a2: "xxx",
},
b:{
    b1: "xxx",
    b2: "xxx"
}
});

wOxxOm

unread,
Oct 22, 2022, 4:20:41 AM10/22/22
to Chromium Extensions, luo...@gmail.com
It depends on how you update the data. If you often update individual values then store them separately.

Stefan Van Damme

unread,
Oct 22, 2022, 5:43:28 AM10/22/22
to Chromium Extensions, wOxxOm, luo...@gmail.com
Hi luo...,

In the long run, it is good to structure your settings. Because when your Chrome extension grows with new features, you can easily find that setting for that feature.
For example, if you create a Night Mode function, you can add the color for "text", "background", "button" and "hyperlink" inside.

But avoid using this way:
chrome.storage.local.set({setting1: "xxx"});
chrome.storage.local.set({setting2: "xxx"});
chrome.storage.local.set({setting3: "xxx"});
Another tip:
Try using "sync" if you want your users to get the same settings for all his other Chrome profiles. It works the same as "local".

Thanks,
Stefan vd

c luo

unread,
Oct 22, 2022, 9:01:31 AM10/22/22
to Chromium Extensions, stefa...@gmail.com, wOxxOm, c luo
I agree to structure the settings. But I have to do the tedious steps to update the setting values. I have to get, merge, then set again. And it seems not performant to get the whole object when you just want use one of the attributes. In Stefan's case, I have to get the whole Night mode object to access the "text". So in which way is chrome.storage designed to use?

wOxxOm

unread,
Oct 22, 2022, 9:35:08 AM10/22/22
to Chromium Extensions, luo...@gmail.com, stefa...@gmail.com, wOxxOm
The storage can be used in any way. There's no preferred method. It depends on your convenience and performance. Performance depends on how often you change individual values. If you do it every second is often. If you do it a couple of times a day then it doesn't matter. However, if the amount of data is big (megabytes) and you only change a subset of the storage then obviously individual setting is better.

Stefan Van Damme

unread,
Oct 22, 2022, 9:48:20 AM10/22/22
to Chromium Extensions, wOxxOm, luo...@gmail.com, Stefan Van Damme
Hi luo...,

Please note that in Google Chrome there is a limit of 512 items for Chrome storage.
In my own Chrome extension projects, I use the flat way.

Thanks,
Stefan vd

wOxxOm

unread,
Oct 22, 2022, 10:14:16 AM10/22/22
to Chromium Extensions, stefa...@gmail.com, wOxxOm, luo...@gmail.com
Yes, but the limit is in chrome.storage.sync, not in chrome.storage.local.

Stefan Van Damme

unread,
Oct 22, 2022, 10:21:36 AM10/22/22
to Chromium Extensions, wOxxOm, Stefan Van Damme, luo...@gmail.com
Hi there,

Indeed, I referred if you use my last tip (use the chrome.storage.sync which is best for the users).

Thanks,
Stefan vd

wOxxOm

unread,
Oct 22, 2022, 10:33:17 AM10/22/22
to Chromium Extensions, stefa...@gmail.com, wOxxOm, luo...@gmail.com
Whether sync storage is best or not depends on the amount of data the users can store, which reiterates my point that there are no absolutes, it depends on the use case, amount of data, frequency of change, whether it's partial or not.
Reply all
Reply to author
Forward
0 new messages