Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
React useCallback hook vs inline function
(version: 0)
Comparing performance of:
Inline vs Hooks
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<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>
Script Preparation code:
function ComponentWithInlineFunction() { const clickMe = evt => evt.preventDefault(); return React.createElement('button', {onClick: clickMe}, 'Click me!'); } function ComponentWithUseCallback() { const clickMe = React.useCallback(evt => evt.preventDefault(), []); return React.createElement('button', {onClick: clickMe}, 'Click me!'); }
Tests:
Inline
ReactDOM.render(React.createElement(ComponentWithInlineFunction), document.getElementById('root'))
Hooks
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
Inline
Hooks
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Inline
285082.2 Ops/sec
Hooks
429352.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its results. **What is being tested?** The benchmark tests two approaches to handling event handling in React applications: 1. **Inline function**: In this approach, the `onClick` handler is defined inline within the component's render method. This means that every time the component is re-rendered, a new function object is created and assigned to the `onClick` property of the button element. 2. **React useCallback hook**: In this approach, the `onClick` handler is defined using the `useCallback` hook from React. The `useCallback` hook memoizes the function so that it's not recreated on every render. **Comparison of options** The two approaches have different performance characteristics: * **Inline function**: This approach creates a new function object every time the component is re-rendered, which can lead to performance issues if the application is complex and has many event handlers. * **React useCallback hook**: By memoizing the function using `useCallback`, React ensures that the function is only recreated when its dependencies change. This can provide better performance, especially in applications with a large number of event handlers. **Pros and cons** * **Inline function**: + Pros: simple to implement, no additional dependencies required. + Cons: creates new function objects on every render, can lead to performance issues. * **React useCallback hook**: + Pros: memoizes the function, reduces unnecessary re-renders, provides better performance. + Cons: requires importing and using the `useCallback` hook from React. **Library and its purpose** In this benchmark, React is used as a library for building the components. The `ReactDOM.render()` method is used to render the React component tree into the DOM. **Special JS feature or syntax** The `useCallback` hook is a special JavaScript feature introduced in React 16.8. It allows you to memoize functions so that they're not recreated on every render. **Other alternatives** If you prefer not to use the `useCallback` hook, you can create your own solution using other techniques, such as: * Creating a function and assigning it to a variable outside of the component's scope. * Using a library like Lodash, which provides a `memoize` function for memoizing functions. However, these alternatives may require more boilerplate code and may not provide the same level of performance benefits as using the `useCallback` hook.
Related benchmarks:
React Hooks vs. Inline
React Hooks vs. Inline (React 16.14.0)
React useCallback hook vs inline function with state react 17
React useCallback vs inline function vs inline handler
Comments
Confirm delete:
Do you really want to delete benchmark?