Skip to content
On this page

Value Comparison

Every store compares the old and the new value on set() with Object.is(). When the values are the same, the store keeps the old value and no listener is called.

You can set store.eq to have custom comparison and avoid unnecessary listeners calls. It is useful for stores with objects or arrays inside, especially for computed stores, which create a new object on every run.

import equal from 'fast-deep-equal/es6'
// Now `$visibleIds` keeps its old array when the new one has the same items
export const $visibleIds = computed($posts, posts => posts.map(post => post.id))
$visibleIds.eq = equal

Three things to keep in mind:

  • The old value can be undefined on the first call, for instance on the first run of a computed store.
  • The function belongs to the store, so all its users share it.
  • store.notify() still calls listeners, even when values are equal.

map() stores have a 2 functions, store.eq for store.set() and store.eqKey for setKey() calls. eqKey receives old value, new value, and key’s name to have a different logic for specific key.

const $settings = map({ theme: 'dark', tags: [] })
$settings.eqKey = equal

Imported from the Nano Stores README for version 1.5.5. Improve this page there.