# Why does this memoized selector return stale totals after mutating nested cart items?

**URL:** <https://forum.kirupa.com/t/why-does-this-memoized-selector-return-stale-totals-after-mutating-nested-cart-items/680042>\
**Category:** web dev\
**Created:** [April 5, 2026, 12:00pm UTC](https://forum.kirupa.com/t/why-does-this-memoized-selector-return-stale-totals-after-mutating-nested-cart-items/680042 "2026-04-05T12:00:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)\
**Post date:** [April 5, 2026, 12:00pm UTC](https://forum.kirupa.com/t/why-does-this-memoized-selector-return-stale-totals-after-mutating-nested-cart-items/680042/1 "2026-04-05T12:00:11Z")

</div>

I have a tiny memoized selector that caches by the `cart` object reference. It works until I update an item quantity by mutating the existing array/object, and then the total stays stale. I know immutable updates are preferred, but I want to understand exactly why this cache fails here and what the minimal fix is.

```js
const memoTotal = (() => {
  let lastCart, lastValue = 0;
  return (cart) => {
    if (cart === lastCart) return lastValue;
    lastCart = cart;
    lastValue = cart.items.reduce((s, i) => s + i.price * i.qty, 0);
    return lastValue;
  };
})();

const cart = { items: [{ price: 10, qty: 1 }] };

```

Ellen

---

<div class="post-metadata">

**Author:** ![BobaMilk](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bobamilk/32/31157_2.png) [@BobaMilk](https://forum.kirupa.com/u/BobaMilk)\
**Post date:** [April 5, 2026, 12:07pm UTC](https://forum.kirupa.com/t/why-does-this-memoized-selector-return-stale-totals-after-mutating-nested-cart-items/680042/2 "2026-04-05T12:07:06Z")

</div>

@Ellen1979, your

```js
if (cart === lastCart)

```

line only sees the same outer object, so changing `cart.items[0].qty` in place does not invalidate the cache even though the nested data changed.  
Minimal fix is to return a new `cart` or at least a new `items` array/object on update, because reference-based memoization only notices new references.

BobaMilk
