From 9dbaf75f465a343e3176a09c9b8f734b681b1d1e Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 8 Jul 2026 14:24:38 +0000 Subject: [PATCH] perf: avoid redundant globalState copy in notifySubscribers Currently, `notifySubscribers` calls `globalState.get()` for each subscriber, which generates a fresh copy of the state object every time due to the internal structure of the `globalState` wrapper. This patch caches `globalState.get()` into a local variable before the loop and passes that cached copy to every subscriber. This low-risk change improves performance by avoiding an allocation per subscriber in an environment where state does not change during the notification loop. Co-authored-by: johnstrand <11484777+johnstrand@users.noreply.github.com> --- src/Squawk.ts | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/Squawk.ts b/src/Squawk.ts index 01a0b5d..cb9266f 100644 --- a/src/Squawk.ts +++ b/src/Squawk.ts @@ -98,6 +98,10 @@ export default function createStore(initialState: Required, useReduxDevToo /** Ensure that subscribers are invoked only once */ const invokedSubscribers = new Set(); + + // Cache the state locally so we don't recreate a copy of it for every subscriber + const currentState = globalState.get(); + const reduceEach = (subscriber: Callback) => { // Check if subscriber has been invoked if (invokedSubscribers.has(subscriber)) { @@ -107,7 +111,7 @@ export default function createStore(initialState: Required, useReduxDevToo // Add the subscriber to the list of invoked subscribers invokedSubscribers.add(subscriber); // and invoke it - subscriber(globalState.get()); + subscriber(currentState); }; // Call the reducer for each list in subscribers