How heavy is reading from a static composition loc...
# compose
v
How heavy is reading from a static composition local? Is it a blazing fast constant time operation, or does it do some tree searching to find the nearest provider? 🤔
👀 1
s
Depends if it is cached (usually it is), but I'd expect something like a map lookup
gratitude thank you 1
r
No, it’s way more expensive than a map lookup
On the same device, a Map read is ~60ns, a CompositionLocal read is ~4,000ns
👆 1
Derived state is the only thing that’s more expensive
s
Static composition local is not a state, so it is cheaper
Assuming it is inside composition and cached, it should be quite cheap, unless I am missing something
r
Ah right sorry I missed the “static” bit
s
tbh, Leland's benchmark indicates that static composition local is still expensive, but I didn't look on what exactly it measures
v
The context here is that I need to determine if I'm inside a preview or not many times in every single composable, I'll just cache the value in a global variable instead to avoid uncertainty in performance cost 😅
s
Are you checking if you're in a preview in your production composable? Why?
v
I was prototyping the viability of a thing. It's a long story! However, I abandoned the idea already 😂