<script src="https://cdnjs.cloudflare.com/ajax/libs/react/18.2.0/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/18.2.0/umd/react-dom.production.min.js"></script>
<div id="root"></div>
function ComponentWithInlineFunction() {
const clickMe = () => {};
return React.createElement('button', {
onClick: clickMe
}, 'Click me!');
}
function ComponentWithUseCallback() {
const clickMe = React.useCallback(() => {}, []);
return React.createElement('button', {
onClick: clickMe
}, 'Click me!');
}
function ComponentWithoutUseMemo() {
const obj = {
x: 1,
y: 1
};
return React.createElement('h1', {
data: obj
}, 'ComponentWithoutUseMemo');
}
function ComponentWithUseMemo() {
const obj = React.useMemo(() => ({
x: 1,
y: 1
}), []);
return React.createElement('h1', {
data: obj
}, 'ComponentWithUseMemo');
}
const ChildWithoutMemo = () => {
return React.createElement('div', null, null);
}
const ChildWithMemo = React.memo(ChildWithoutMemo);
function ComponentWithoutMemorizedChild() {
const [count, setCount] = React.useState(0);
React.useEffect(() => {
if (count < 10000) {
setCount(c => c + 1);
}
})
return React.createElement(ChildWithoutMemo, null, null);
}
function ComponentWithMemorizedChild() {
const [count, setCount] = React.useState(0);
React.useEffect(() => {
if (count < 10000) {
setCount(c => c + 1);
}
})
return React.createElement(ChildWithMemo, null, null);
}
ReactDOM.render(React.createElement(ComponentWithInlineFunction), document.getElementById('root'))
ReactDOM.render(React.createElement(ComponentWithUseCallback), document.getElementById('root'))
ReactDOM.render(React.createElement(ComponentWithoutUseMemo), document.getElementById('root'))
ReactDOM.render(React.createElement(ComponentWithUseMemo), document.getElementById('root'))
ReactDOM.render(React.createElement(ComponentWithoutMemorizedChild), document.getElementById('root'))
ReactDOM.render(React.createElement(ComponentWithMemorizedChild), document.getElementById('root'))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Inline Function | |
Hooks | |
Without useMemo | |
With useMemo | |
Child without memo | |
Child with memo |
Test name | Executions per second |
---|---|
Inline Function | 505580.5 Ops/sec |
Hooks | 581778.1 Ops/sec |
Without useMemo | 450433.7 Ops/sec |
With useMemo | 579485.1 Ops/sec |
Child without memo | 398920.2 Ops/sec |
Child with memo | 420689.7 Ops/sec |
Let's break down the benchmark and its components.
Benchmark Definition JSON
The benchmark definition is a JSON object that contains information about the test cases. In this case, there are six test cases:
Inline Function
: Tests rendering a React component with an inline function as the click handler.Hooks
: Tests rendering a React component using the useCallback
hook to memoize the click handler.Without useMemo
: Tests rendering a React component without using the useMemo
hook.With useMemo
: Tests rendering a React component using the useMemo
hook to memoize a computed value.Child without memo
: Tests rendering a child component without memoization.Child with memo
: Tests rendering a child component with memoization.Options Compared
The benchmark compares three options:
useCallback
hook to memoize the click handler in the React component.useMemo
hook or any form of memoization in the React component.Pros and Cons
Library and Purpose
The benchmark uses React 18.2.0 as the library, which includes several new features such as improved memoization options (e.g., useCallback
, useMemo
).
Special JS Features
This benchmark doesn't use any special JavaScript features like async/await or Promises, but it does use modern ES6+ syntax.
Device and OS
The benchmark runs on a desktop machine with macOS 10.15.7.
Results
The results show the number of executions per second for each test case:
Hooks
: 581,778.0625With useMemo
: 579,485.0625Inline Function
: 505,580.5Without useMemo
: 450,433.6875Child with memo
: 420,689.65625Child without memo
: 398,920.15625These results indicate that using the useCallback
hook and/or useMemo
can lead to significant performance improvements compared to not using them at all.