When working on performance-sensitive code (query observers, hydration, render-triggering state, hot utilities), ensure you only do the necessary work and no redundant allocations/wrappers, while also preventing leaks.

Apply these rules:

Example (single Set allocation in a hot utility):

function difference<T>(array1: T[], array2: T[]): T[] {
  const set2 = new Set(array2)
  return array1.filter((x) => !set2.has(x))
}

Example (wrap fetch once when a persister exists):

if (context.options.persister) {
  context.fetchFn = () =>
    context.options.persister!(fetchFn as any, {
      queryKey: context.queryKey,
      meta: context.options.meta,
      signal: context.signal,
    })
}