Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test UseRef Sagar
(version: 0)
Comparing performance of:
No useMemo vs useMemo vs useRef
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() }); } function UseRefAlone(props) { const handler = React.useRef({ ...a, c: "!!" }); return React.createElement(ChildComponent, { className: handler.current.toString() }); } var propCases = [{ handler: function() { return Math.random(); }, wait: 100 }, { handler: function() { return Math.random(); }, wait: 200 }, { handler: function() { return Math.random(); }, wait: 300 }]; propCases.push(propCases[1]);
Tests:
No useMemo
propCases.forEach((props) => { ReactDOM.render(React.createElement(NoMemo, props, null), root); });
useMemo
propCases.forEach((props) => { ReactDOM.render(React.createElement(UseMemoAlone, props, null), root); });
useRef
propCases.forEach((props) => { ReactDOM.render(React.createElement(UseRefAlone, props, null), root); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
No useMemo
useMemo
useRef
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:127.0) Gecko/20100101 Firefox/127.0
Browser/OS:
Firefox 127 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
No useMemo
10896.6 Ops/sec
useMemo
9942.6 Ops/sec
useRef
9381.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Explanation** The provided benchmark is designed to compare the performance of three different approaches in React.js: `UseMemo`, `UseRef`, and not using memoization or ref at all (`NoMemo`). **Options Compared** 1. **UseMemo**: This approach uses the `React.useMemo` hook to memoize a function that returns an object with properties from `a` and another property `c`. The function is only recreated when `props` change, and its result is cached. 2. **UseRef**: This approach uses the `React.createRef` method to create a reference variable that holds a value. In this case, the initial value is set to an object with properties from `a` and another property `c`. When `props` change, the function is called again, but since the ref's current value hasn't changed, it doesn't re-run. 3. **NoMemo**: This approach does not use memoization or ref at all. It simply returns a new object with properties from `a` and another property `c`, every time `props` change. **Pros and Cons** * **UseMemo**: + Pros: Easy to implement, reduces unnecessary re-renders. + Cons: Can lead to performance issues if the memoized function is computationally expensive or has a complex dependency list. * **UseRef**: + Pros: Can be more efficient than `useMemo` when dealing with simple dependencies, as it only runs when the ref's value changes. + Cons: Requires manual management of the ref's current value, which can lead to bugs if not done correctly. * **NoMemo**: + Pros: Simple and straightforward approach. + Cons: Can lead to unnecessary re-renders, especially if the component has many dependencies. **Library Usage** The benchmark uses `lodash` as a dependency. While it's not directly related to React or memoization, its presence might affect performance due to additional function calls. **Special JS Features/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. The code is straightforward and uses standard JavaScript features like functions, objects, and arrays. **Alternatives** Other approaches for memoization in React include: 1. `React.useCallback`: Memoizes a function, which can be useful when the dependency list is short. 2. `React.useImperativeHandle`: Memoizes a handle, which can be used to improve performance when dealing with complex dependencies. 3. Custom hooks: Developers can create their own custom hooks using `React.createHook` and memoize data or functions accordingly. Keep in mind that each approach has its trade-offs, and the choice of which one to use depends on the specific requirements and constraints of your project.
Related benchmarks:
useMemo and memo
MemoTest
useMemo and memoa
Test UseRef
Comments
Confirm delete:
Do you really want to delete benchmark?