Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Memoization + useCallback
(version: 1)
Comparing performance of:
Inline + NonMemoized vs Inline + Memoized vs useCallback + NonMemoized vs useCallback + Memoized
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/17.0.2/umd/react.production.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/17.0.2/umd/react-dom.production.min.js"></script> <div id="root"></div>
Script Preparation code:
function ComponentWithInlineFunctionAndNonMemoizedButton() { const [state, setState] = React.useState(0); const clickMe = evt => { evt.preventDefault(); setState(function(existingState){ // Trigger re-render each time return existingState + 1; }); } return React.createElement(NonMemoizedButton, { onClick: clickMe }, 'Click me!'); } function ComponentWithInlineFunctionAndMemoizedButton() { const [state, setState] = React.useState(0); const clickMe = evt => { evt.preventDefault(); setState(function(existingState){ // Trigger re-render each time return existingState + 1; }); } return React.createElement(MemoizedButton, { onClick: clickMe }, 'Click me!'); } function ComponentWithUseCallbackAndNonMemoizedButton() { const [state, setState] = React.useState(0); const clickMe = React.useCallback(evt => { evt.preventDefault(); setState(function(existingState){ // Trigger re-render each time return existingState + 1; }); }, []); return React.createElement(NonMemoizedButton, { onClick: clickMe }, 'Click me!'); } function ComponentWithUseCallbackAndMemoizedButton() { const [state, setState] = React.useState(0); const clickMe = React.useCallback(evt => { evt.preventDefault(); setState(function(existingState){ // Trigger re-render each time return existingState + 1; }); }, []); return React.createElement(MemoizedButton, { onClick: clickMe }, 'Click me!'); } function NonMemoizedButton (props) { const { a, b, c, d, e, f, g, h, i, j, k, l, m, n, onClick } = props; const classes = ["some", "joined", "words", "in", "an", "array"] return React.createElement('button', { className: classes.join(" "), onClick: onClick }, 'Click me!'); } const MemoizedButton = React.memo(function MemoizedWrapperComponent (props) { const { a, b, c, d, e, f, g, h, i, j, k, l, m, n, onClick } = props; const classes = ["some", "joined", "words", "in", "an", "array"] return React.createElement('button', { className: classes.join(" "), onClick: onClick }, 'Click me!'); });
Tests:
Inline + NonMemoized
ReactDOM.render(React.createElement(ComponentWithInlineFunctionAndNonMemoizedButton), document.getElementById('root'))
Inline + Memoized
ReactDOM.render(React.createElement(ComponentWithInlineFunctionAndMemoizedButton), document.getElementById('root'))
useCallback + NonMemoized
ReactDOM.render(React.createElement(ComponentWithUseCallbackAndNonMemoizedButton), document.getElementById('root'))
useCallback + Memoized
ReactDOM.render(React.createElement(ComponentWithUseCallbackAndMemoizedButton), document.getElementById('root'))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Inline + NonMemoized
Inline + Memoized
useCallback + NonMemoized
useCallback + Memoized
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition** The benchmark definition is a JSON object that defines a set of test cases. It contains two main parts: 1. **Script Preparation Code**: This code snippet represents the React component used in each test case. The components are defined using JavaScript functions, which are then wrapped with various optimization techniques (more on this later). 2. **Html Preparation Code**: This code snippet sets up the HTML environment for the benchmark, including importing React and ReactDOM. **Optimization Techniques** The script preparation code uses three optimization techniques: 1. **Inline Functions**: The `clickMe` function is defined inline within the component's scope. 2. **Non-Memoized Buttons**: The `NonMemoizedButton` component does not use any memoization technique to optimize its rendering. 3. **Memoized Buttons**: The `MemoizedButton` component uses React's built-in `React.memo()` higher-order component (HOC) to memoize its rendering. 4. **useCallback with Non-Memoized Buttons**: The `ComponentWithUseCallbackAndNonMemoizedButton` component uses the `React.useCallback()` hook without any arguments, effectively memoizing the `clickMe` function. 5. **useCallback with Memoized Buttons**: The `ComponentWithUseCallbackAndMemoizedButton` component uses the `React.useCallback()` hook with an empty array as its dependency, effectively memoizing the `clickMe` function and making it only re-run when the dependencies change. **Pros and Cons of Each Approach** Here's a brief summary of the pros and cons of each optimization technique: 1. **Inline Functions**: Pros: Simplifies code, reduces memory allocation. Cons: Can lead to slower performance due to larger code size. 2. **Non-Memoized Buttons**: Pros: None notable. Cons: Does not optimize rendering, can be slow. 3. **Memoized Buttons**: Pros: Optimizes rendering by caching component output. Cons: May increase memory usage if not used carefully. 4. **useCallback with Non-Memoized Buttons**: Pros: Memoizes the `clickMe` function without affecting its behavior. Cons: Does not optimize rendering, can be slow. 5. **useCallback with Memoized Buttons**: Pros: Optimizes rendering by memoizing the `clickMe` function and reducing unnecessary re-renders. Cons: May increase memory usage if not used carefully. **Benchmark Test Cases** The benchmark test cases are designed to measure the performance of each optimization technique in different scenarios: 1. **Inline + Non-Memoized Buttons**: Measures the performance of an inline function without memoization. 2. **Inline + Memoized Buttons**: Measures the performance of an inline function with memoization. 3. **useCallback + Non-Memoized Buttons**: Measures the performance of a `useCallback` hook without memoization. 4. **useCallback + Memoized Buttons**: Measures the performance of a `useCallback` hook with memoization. The benchmark results show the number of executions per second for each test case, providing insight into the performance characteristics of each optimization technique. **Device and Browser Variations** The benchmark results also include data on various devices and browsers, which can help identify platform-specific performance variations.
Related benchmarks:
React Hooks vs. Inline, useMemo, memo
React useCallback hook vs inline function with state react 17
Memoization + useCallback (II)
React useCallback
Comments
Confirm delete:
Do you really want to delete benchmark?