<script crossorigin src="https://unpkg.com/react@18/umd/react.production.min.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@18/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 | 591621.6 Ops/sec |
Hooks | 669048.1 Ops/sec |
Without useMemo | 516036.4 Ops/sec |
With useMemo | 630078.1 Ops/sec |
Child without memo | 451535.5 Ops/sec |
Child with memo | 493341.6 Ops/sec |
Measuring the performance of different approaches in React Hooks can be an interesting benchmarking exercise.
What is being tested?
The benchmark compares the performance of three approaches:
useCallback
or useMemo
.useCallback
) to memoize the click handler function.React.useMemo
to optimize the rendering of a component that contains an object.Options compared
The benchmark compares the performance of these three approaches, which can be summarized as follows:
useCallback
and useMemo
to reduce unnecessary re-renders.Pros and Cons
Here are some pros and cons of each approach:
Library and its purpose
The React
library is used in this benchmark. It's a JavaScript library for building user interfaces and is widely used in web development.
Special JS feature or syntax
This benchmark uses some special JavaScript features:
useMemo
: Used in the With useMemo approach.Overall, this benchmark is designed to help developers understand the trade-offs between different approaches when it comes to performance optimization in React applications.