Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
useMemo and memoa
(version: 0)
Comparing performance of:
No Memo vs UseMemoAlone
Created:
one year ago
by:
Guest
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 a = { a: "hello", b: "world" } function UseMemoAlone(props) { const handler = React.useMemo(() => { return { ...a, c: "!!" } }, []); return React.createElement(ChildComponent, { className: handler.toString() }); } function NoMemo(props) { const handler = { ...a, c: "!!" }; return React.createElement(ChildComponent, { className: handler.toString() }); } 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); });
UseMemoAlone
propCases.forEach((props) => { ReactDOM.render(React.createElement(UseMemoAlone, props, null), root); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
No Memo
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_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
No Memo
19324.8 Ops/sec
UseMemoAlone
15583.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what is being tested in the provided JSON. The benchmark compares two approaches to using React's `useMemo` hook: 1. **No Memo**: This approach does not use `useMemo`. Instead, it creates a new object with additional properties every time it's rendered. The code uses the spread operator (`...`) to create a new object with the existing properties of `a` and an additional property `c`. 2. **UseMemoAlone**: This approach uses `useMemo` to memoize the creation of a new object with additional properties. The `useMemo` hook is used to compute a value (in this case, a new object) only when the dependencies change (which they don't in this example). The resulting memoized value is then used as a class name for a React component. **Pros and Cons:** * **No Memo**: + Pros: Simple, easy to understand. + Cons: Creates a new object every time it's rendered, which can lead to performance issues due to the overhead of creating a new object. * **UseMemoAlone**: + Pros: Reduces the number of objects created by memoizing the computation, which can improve performance. + Cons: Requires using `useMemo`, which adds complexity and may not be familiar to all developers. Other considerations: * The benchmark uses React's `ReactDOM.render` method to render the components, which means that the rendering process is affected by the browser's rendering engine and JavaScript engine. * The benchmark pushes a duplicate array onto `propCases` after defining it, which can lead to unexpected behavior or incorrect results. This seems like an error on the part of the benchmark creator. Now, let's talk about some special JavaScript features: * `...a` is using the spread operator to create a new object with existing properties from `a`. This is a shorthand way of creating a new object with the same properties as an existing object. * `!!` is a bitwise NOT operator that converts its operand to a boolean value. In this case, it's used to create a string `"!!"`. * `React.createElement` is a function that creates a React element from a template string. As for libraries and dependencies, the benchmark uses: * React: The main library being tested. * Lodash: A utility library that provides functions like `lodash.min`, which is used in the benchmark preparation code. Finally, I'll mention that there are other alternatives to using `useMemo` alone. Some options include: * Using a separate function to compute the memoized value and calling it only when necessary. * Using a more advanced memoization technique, such as using a cache or a dependency array with more sophisticated logic. * Avoiding the use of `useMemo` altogether by reordering dependencies or using other optimization techniques. However, without more context, it's difficult to recommend alternative approaches that would be applicable in this specific benchmark.
Related benchmarks:
React Memo or No Memo
useMemo and memo
memosy
MemoTest
Comments
Confirm delete:
Do you really want to delete benchmark?