Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
useMemo and memo
(version: 0)
Comparing performance of:
No Memo vs UseMemoAndOptimizedChild vs UseMemoAlone
Created:
5 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 ChildComponent(props) { return React.createElement("div", props); } const OptimizedChildComponent = React.memo(ChildComponent); function UseMemoAndOptimizedChild(props) { const handler = React.useMemo(() => { return _.debounce(props.handler, props.wait); }, [props.handler, props.wait]); return React.createElement(OptimizedChildComponent, { className: handler() }); } function UseMemoAlone(props) { const handler = React.useMemo(() => { return _.debounce(props.handler, props.wait); }, [props.handler, props.wait]); return React.createElement(ChildComponent, { className: handler() }); } function NoMemo(props) { const handler = _.debounce(props.handler, props.wait); return React.createElement(ChildComponent, { className: handler() }); } var propCases = [{ handler: function() { return Math.random(); }, wait: 100 }, { handler: function() { return Math.random(); }, wait: 200 }]; propCases.push(propCases[1]);
Tests:
No Memo
propCases.forEach((props) => { ReactDOM.render(React.createElement(NoMemo, props, null), root); });
UseMemoAndOptimizedChild
propCases.forEach((props) => { ReactDOM.render(React.createElement(UseMemoAndOptimizedChild, props, null), root); });
UseMemoAlone
propCases.forEach((props) => { ReactDOM.render(React.createElement(UseMemoAlone, props, null), root); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
No Memo
UseMemoAndOptimizedChild
UseMemoAlone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:132.0) Gecko/20100101 Firefox/132.0
Browser/OS:
Firefox 132 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
No Memo
11800.3 Ops/sec
UseMemoAndOptimizedChild
13356.8 Ops/sec
UseMemoAlone
11840.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** The provided JSON represents a JavaScript microbenchmark test case on the MeasureThat.net website. The benchmark tests three different approaches for memoizing (caching) function calls in React components: `UseMemoAndOptimizedChild`, `UseMemoAlone`, and `NoMemo`. **Tested Options** 1. **`UseMemoAndOptimizedChild`**: This approach uses `React.memo` to wrap the component and also uses `React.useMemo` to memoize the debounced function call. * Pros: + Caches the entire component instance, which can be beneficial for complex components. + Uses `React.memo` to optimize the rendering of the component. * Cons: + May lead to unnecessary re-renders if the cached component instance is not updated correctly. 2. **`UseMemoAlone`**: This approach only uses `React.useMemo` to memoize the debounced function call, without wrapping the component with `React.memo`. * Pros: + Optimizes the rendering of individual function calls, reducing unnecessary re-renders. * Cons: + May lead to more complex optimization logic and caching issues if not implemented correctly. 3. **`NoMemo`**: This approach does not use memoization at all, relying on the `_.debounce` function from Lodash to debounce the function call. * Pros: + Simplest implementation with minimal overhead. * Cons: + May lead to excessive re-renders or performance issues if the debounced function is called frequently. **Library and Special JS Features** 1. **Lodash**: The `_.debounce` function from Lodash is used in all three approaches to debounce the function call. Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like string manipulation, array manipulation, and more. 2. **React**: The benchmark uses React as the rendering engine, utilizing its `ReactDOM.render` method to render the components. **Special JS Features** 1. **React Memoization**: The use of `React.memo` and `React.useMemo` enables memoization in the `UseMemoAndOptimizedChild` approach, which caches the component instance and individual function calls. 2. **Debouncing**: The use of Lodash's `_.debounce` function introduces debouncing logic to limit the frequency of function calls. **Alternative Approaches** Other approaches that could be considered for memoization in React components include: 1. **Using a custom wrapper component**: Instead of using `React.memo`, you could create a custom wrapper component that handles memoization and optimization. 2. **Implementing custom caching mechanisms**: You could implement your own caching mechanism using JavaScript objects or other data structures to store cached values. 3. **Utilizing third-party libraries**: Other libraries like `react-pizza` or `recompose` provide various tools for optimizing and memoizing React components. Keep in mind that the choice of approach depends on the specific requirements and constraints of your application, as well as your personal preference and expertise.
Related benchmarks:
React Memo or No Memo
memosy
MemoTest
useMemo and memoa
Comments
Confirm delete:
Do you really want to delete benchmark?