<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="inline"></div>
<div id="hooks"></div>
function ComponentWithInlineFunction() {
const [__, forceRerender] = React.useState();
React.useEffect(() => {
window.forceRerenderInline = forceRerender;
}, []);
const clickMe = evt => evt.preventDefault();
return React.createElement('button', {onClick: clickMe}, 'Click me!');
}
ReactDOM.render(React.createElement(ComponentWithInlineFunction), document.getElementById('inline'))
function ComponentWithUseCallback() {
const [__, forceRerender] = React.useState();
React.useEffect(() => {
window.forceRerenderHooks = forceRerender;
}, []);
const clickMe = React.useCallback(evt => evt.preventDefault(), []);
return React.createElement('button', {onClick: clickMe}, 'Click me!');
}
ReactDOM.render(React.createElement(ComponentWithUseCallback), document.getElementById('hooks'))
window.forceRerenderInline({})
window.forceRerenderHooks({})
ReactDOM.render(React.createElement(ComponentWithInlineFunction), document.getElementById('inline'))
ReactDOM.render(React.createElement(ComponentWithUseCallback), document.getElementById('hooks'))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Inline Updates | |
Hooks Updates | |
Inline Mount | |
Hooks Mount |
Test name | Executions per second |
---|---|
Inline Updates | 888794.8 Ops/sec |
Hooks Updates | 1080303.6 Ops/sec |
Inline Mount | 759676.8 Ops/sec |
Hooks Mount | 907568.1 Ops/sec |
Let's dive into the benchmark and explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The provided JSON represents a JavaScript microbenchmark that compares two approaches for handling updates in React applications:
useState
and useEffect
) to manage state and side effects.Script Preparation Code
The script preparation code defines two components, ComponentWithInlineFunction
and ComponentWithUseCallback
, which will be used as the test cases for each benchmark. These components:
window.forceRerenderInline
) or React Hooks (window.forceRerenderHooks
), respectively, to manage state updates.Html Preparation Code
The HTML preparation code sets up the necessary environment for running the benchmark. It includes:
div
elements with IDs "inline" and "hooks", which will be used as targets for rendering the components.Test Cases
There are four test cases, each representing a different aspect of the benchmark:
Library and Purpose
Special JS Feature or Syntax
None of the test cases use special JavaScript features or syntax beyond what's required for React and its Hooks. However, it's worth noting that using useCallback
(not explicitly used in this benchmark) can help prevent unnecessary re-renders by memoizing functions.
Pros and Cons
Here are some general pros and cons of each approach:
useEffect
) or updates to internal state.Alternatives
Other alternatives for handling updates in React applications include:
componentDidMount
, componentDidUpdate
) can be an alternative to hooks.Overall, the benchmark provides a useful comparison of two approaches for handling updates in React applications. The results will help developers and engineers understand the relative performance characteristics of each approach in different scenarios.