Performance Tips β
Atomix is designed to be lightweight and performant by default β but you can get even more out of it with the right patterns.
This guide covers how to avoid unnecessary work and ensure fast reactivity at scale.
π― Use Selectors to Limit Re-Renders β
Instead of subscribing to the entire atom:
ts
const state = useAtom((state) => state); // β Subscribes to whole object
Select only what you need:
ts
const count = useAtom((s) => s.count); // β
This reduces unnecessary re-renders and improves component isolation.
π§ Avoid Storing Derived State β
Donβt do this:
ts
createAtom({ count: 0, doubled: 0 }); // β
Do this instead:
ts
const doubled = useAtom((state) => state.count * 2); // β
Derived state should be computed, not stored.
π§ Keep Atom State Shallow β
Avoid deeply nested state objects, since changes to deep values are harder to track and optimize.
ts
// Instead of this:
createAtom({ user: { profile: { name: "Naol" } } }); // β
// Do this:
createAtom({ userName: "Naol" }); // β
π Batch Updates (WIP) β
In future versions, Atomix may support batched updates to reduce render cycles. For now, just group setState calls carefully.
π Summary β
Tip | Benefit |
---|---|
Use selectors | Reduce unnecessary renders |
Avoid derived in state | Keep logic declarative |
Split state by concern | Easier to debug and test |