Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perf: improve useMemoized callback execution time #5190

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ Notes: web developers are advised to use [`~` (tilde range)](https://github.com/
- Fixes [#5175](https://github.com/microsoft/BotFramework-WebChat/issues/5175). `PrecompiledGlobalize.js` is emitted instead of `.cjs`, by [@compulim](https://github.com/compulim) in PR [#5181](https://github.com/microsoft/BotFramework-WebChat/pull/5181)
- Improved performance for `BasicTranscript`, in PR [5183](https://github.com/microsoft/BotFramework-WebChat/pull/5183), by [@OEvgeny](https://github.com/OEvgeny)
- Fixed potential memory usage issues caused by `useActivitiesWithRenderer`, in PR [5183](https://github.com/microsoft/BotFramework-WebChat/pull/5183), by [@OEvgeny](https://github.com/OEvgeny)
- Improved performance for `useMemoized`, in PR [5190](https://github.com/microsoft/BotFramework-WebChat/pull/5190), by [@OEvgeny](https://github.com/OEvgeny)

### Changed

Expand Down
29 changes: 21 additions & 8 deletions packages/component/src/hooks/internal/useMemoized.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,30 @@ export default function useMemoized<TFinal, TArgs>(fn: Fn<TArgs, TFinal>, deps:
nextCacheRef.current = [];

const memoizedFn = (...args) => {
const { current: fn } = fnRef;
const { current: cache } = cacheRef;
const { current: nextCache } = nextCacheRef;
const { result } = [...cache, ...nextCache].find(
const fn = fnRef.current;
const cache = cacheRef.current;
const nextCache = nextCacheRef.current;

const cached = cache.find(
({ args: cachedArgs }) =>
args.length === cachedArgs.length && args.every((arg, index) => Object.is(arg, cachedArgs[+index]))
) || { result: fn(...args) };
// index is guarented to be a number here
// eslint-disable-next-line security/detect-object-injection
args.length === cachedArgs.length && args.every((arg, index) => Object.is(arg, cachedArgs[index]))
OEvgeny marked this conversation as resolved.
Show resolved Hide resolved
);
if (cached) {
cached.args = args;
nextCache.push(cached);
return cached.result;
}

nextCache.push({ args, result });
const nextCached = {
args,
result: fn(...args)
};
nextCache.push(nextCached);
cache.push(nextCached);

return result;
return nextCached.result;
};

return memoizedFn;
Expand Down
Loading