Best practice derived atom #612
-
I'm using a derived atom where I use the same value of another atom multiple times. Example: const feedSelectedAtom = atom(get => get(selectedImportAtom)?.type == 'XML' || get(selectedImportAtom)?.type == 'CSV' || get(selectedImportAtom)?.type == 'JSON') vs const feedSelectedAtom = atom(get => {
const selectedImport = get(selectedImportAtom)
return selectedImport?.type == 'XML' || selectedImport?.type == 'CSV' || selectedImport?.type == 'JSON'
}) Both methods work of course but is there any noticable performance difference and would you recommend one above the other? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
Technically, I would probably do the second example in your use case. The code tells it's reading only one atom. (In the first example, you would need to double check if all |
Beta Was this translation helpful? Give feedback.
Technically,
get
comes with a few function calls, one Map lookup, one Set addition, and invalidation check whose cost depends (hard to estimate, and there might be room for optimization).So, having the result in a local variable would be better.
But, it's not noticeable unless it's an extreme case.
We should care more about the code readability and maybe bundle size.
I would probably do the second example in your use case. The code tells it's reading only one atom. (In the first example, you would need to double check if all
get
s take the same atom.)