React Hooks vs. Inline
Date tested:
11 months ago
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Test name
Executions per second
Inline
497981.4 Ops/sec
Hooks
624482.3 Ops/sec
Benchmark definition (click to collapse):
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'))
Open this result on MeasureThat.net