Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test: React useCallback vs notUseCallback
(version: 0)
Comparing performance of:
Without useCallback vs With useCallback
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!doctype html> <html> <head> <title>This is the title of the webpage!</title> </head> <body> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.13.1/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.13.1/umd/react-dom.production.min.js"></script> <div id="root"></div> </body> </html>
Script Preparation code:
function ComponentWithOutUseCallback() { const testFunction = (evt) => evt.preventDefault() return React.createElement('button', {onClick: testFunction}, 'Test click'); } function ComponentWithUseCallback() { const testFunction = React.useCallback(evt => evt.preventDefault(), []); return React.createElement('button', {onClick: testFunction}, 'Test click'); }
Tests:
Without useCallback
ReactDOM.render(React.createElement(ComponentWithOutUseCallback), document.getElementById('root'))
With useCallback
ReactDOM.render(React.createElement(ComponentWithUseCallback), document.getElementById('root'))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Without useCallback
With useCallback
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Without useCallback
923098.1 Ops/sec
With useCallback
1521279.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided benchmark. **What is tested?** The benchmark tests two approaches to create a React component: `ComponentWithOutUseCallback` and `ComponentWithUseCallback`. Both components have an `onClick` event handler, but they differ in how this handler is created. **Options compared** The two options being compared are: 1. **Without useCallback**: In this approach, the `onClick` event handler is defined directly within the component function. This means that the closure (the scope of the variable) is not explicitly managed. 2. **With useCallback**: In this approach, the `onClick` event handler is created using the `React.useCallback` hook. This hook returns a memoized version of the event handler, which is updated only when the dependencies change. **Pros and Cons** ### Without useCallback Pros: * Simpler implementation * No additional dependency management required Cons: * The closure (the scope of the variable) might be recreated unnecessarily on each render, leading to unnecessary function calls. * If the component's state or props change, the event handler will need to be re-created, which can lead to performance issues. ### With useCallback Pros: * Memoization is used to prevent unnecessary re-creations of the event handler * The dependency array in `useCallback` ensures that the event handler is updated only when necessary Cons: * More complex implementation due to the use of a hook * Additional overhead for creating and managing the memoized function **Library/Functionality** In this benchmark, the following libraries are used: * **React**: The JavaScript library for building user interfaces. * **ReactDOM**: A library that provides DOM-related functionality for React applications. The `useCallback` hook is a part of React and is used to manage dependencies in functional components. **Special JS Feature/Syntax** This benchmark uses the special feature of JavaScript called "closures". Closures are functions that have access to their own scope and can use variables from that scope even when the function is called outside its original context. In this case, the `onClick` event handler is a closure that captures the variables defined in its outer scope. **Other Alternatives** If you're looking for alternatives to `useCallback`, you might consider using other hooks like `useMemo` or `useRef`. However, these hooks have different use cases and might not provide the same benefits as `useCallback` in this scenario. Additionally, if you want to avoid using React's hooks altogether, you could implement your own memoization mechanism using a separate function that captures dependencies. However, this would likely require more code and might not be as efficient or scalable.
Related benchmarks:
React 18.2 useCallback hook vs. function
ROUND1: React useCallback vs notUseCallback
React17: React useCallback vs notUseCallback
React useCallback hook vs. function 18
Comments
Confirm delete:
Do you really want to delete benchmark?