Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
React Memo or No Memo
(version: 2)
Comparing performance of:
Memo vs No Memo
Created:
6 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id="root"></div> <script src="https://unpkg.com/react@16/umd/react.development.js"></script> <script src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"></script> <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
Script Preparation code:
function Memo(props) { const handler = React.useMemo(() => { return _.debounce(props.handler, props.wait); }, [props.handler, props.wait]); return React.createElement("div", { className: handler() }); } function NoMemo(props) { const handler = _.debounce(props.handler, props.wait); return React.createElement("div", { className: handler() }); } var propCases = [{ handler: function() { return Math.random(); }, wait: 100 }, { handler: function() { return Math.random(); }, wait: 200 }]; propCases.push(propCases[1]);
Tests:
Memo
propCases.forEach((props, i) => { ReactDOM.render(React.createElement(Memo, props, null), root); });
No Memo
propCases.forEach((props) => { ReactDOM.render(React.createElement(NoMemo, props, null), root); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Memo
No Memo
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36
Browser/OS:
Chrome 144 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Memo
33242.7 Ops/sec
No Memo
31933.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Definition JSON** The benchmark definition represents two different approaches to optimizing React components: 1. **Memo**: This approach uses `React.useMemo` to memoize a function that debounces an event handler using Lodash's `_debounce` function. The memoized function is only recalculated when the dependencies (`props.handler` and `props.wait`) change. 2. **No Memo**: This approach directly uses Lodash's `_debounce` function on the event handler without memoizing it. **Options being compared** The two options being compared are: * Using `React.useMemo` to memoize a debounced function (Memo) * Not using `React.useMemo` and instead applying debouncing directly to the event handler (No Memo) **Pros and Cons of each approach** 1. **Memo (using React.useMemo)**: * Pros: + Reduces unnecessary recalculations by memoizing the debounced function, which only depends on its dependencies. + Improves performance by avoiding redundant computations. * Cons: + May introduce additional overhead due to the creation and management of a new scope for the memoized function. 2. **No Memo (not using React.useMemo)**: * Pros: + Avoids creating an additional scope, which might reduce overhead. * Cons: + Recalculates the debounced function every time it's called, potentially leading to performance issues. **Library and purpose** The Lodash library is used in both approaches. Specifically, `_debounce` is a function that takes a function as an argument and returns a new function that wraps the original function with a delay. In this case, `_.debounce(props.handler, props.wait)` creates a debounced version of the event handler, which only fires after the specified wait time has passed. **Special JavaScript features or syntax** This benchmark uses the following JavaScript features: * ES6 classes (`function Memo()` and `function NoMemo()`) * React Hooks (specifically, `React.useMemo`) * Lodash functions (`_.debounce`) These features are widely supported in modern browsers and JavaScript environments. **Other alternatives** Other approaches to debouncing event handlers might include: * Using a library like Debounce.js or Moment.js * Implementing debouncing manually using a loop or recursion * Using async/await or promise chaining to delay the execution of the handler However, React Hooks and Lodash's `_debounce` provide a concise and efficient way to implement debouncing in this specific use case.
Related benchmarks:
useMemo and memo
memosy
React.memo vs no React.memo
Memoization + useCallback (II)
Comments
Confirm delete:
Do you really want to delete benchmark?