Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
React useCallback, useMemo vs inline filter
(version: 1)
Comparing performance of:
ComponentUseCallback vs ComponentUseMemo vs ComponentUseInline
Created:
2 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!doctype html> <html> <head> <title>This is the title of the webpage!</title> </head> <body> <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> </body> </html>
Script Preparation code:
var KEYWORDS = "the quick brown fox jumped over the lazy dog".split(' '); function ComponentUseCallback({ keywords }) { const items = React.useCallback(() => { return keywords.filter((v) => !!v); }, [keywords]); return React.createElement('div', {}, items().join(',')); } function ComponentUseMemo({ keywords }) { const items = React.useMemo(() => { return keywords.filter((v) => !!v); }, [keywords]); return React.createElement('div', {}, items.join(',')); } function ComponentUseInline({ keywords }) { const items = keywords.filter((v) => !!v); return React.createElement('div', {}, items.join(',')); }
Tests:
ComponentUseCallback
ReactDOM.render(React.createElement(ComponentUseCallback, { keywords: KEYWORDS }), document.getElementById('root'))
ComponentUseMemo
ReactDOM.render(React.createElement(ComponentUseMemo, { keywords: KEYWORDS }), document.getElementById('root'))
ComponentUseInline
ReactDOM.render(React.createElement(ComponentUseInline, { keywords: KEYWORDS }), document.getElementById('root'))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
ComponentUseCallback
ComponentUseMemo
ComponentUseInline
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 dive into the world of JavaScript benchmarks! The provided JSON represents a benchmark test for React components, specifically comparing three different approaches: `useCallback`, `useMemo`, and inline filtering. **What is tested?** In this benchmark, we're testing the performance of three React components that filter an array of keywords. The components are created using the `ComponentUseCallback`, `ComponentUseMemo`, and `ComponentUseInline` functions. * `ComponentUseCallback` uses the `React.useCallback` hook to memoize a function that filters the keywords. * `ComponentUseMemo` uses the `React.useMemo` hook to memoize a value that filters the keywords. * `ComponentUseInline` uses a simple inline filter to achieve the same result. **Options compared** The benchmark compares the performance of these three approaches: 1. **useCallback**: This approach uses a function created with `React.useCallback` as a memoized value. The function is recreated whenever the dependencies change (in this case, the `keywords` array). 2. **useMemo**: This approach uses a value computed with `React.useMemo` as a memoized value. The value is recomputed whenever any of its dependencies change. 3. **Inline filter**: This approach uses a simple inline filter to achieve the same result as the other two approaches. **Pros and cons of each approach** * **useCallback**: + Pros: Can be useful when you need to create a memoized function that depends on external state. + Cons: Creates a new function on every render, which can lead to performance issues if the dependencies change frequently. * **useMemo**: + Pros: Reuses the computed value whenever it's needed, which can improve performance. + Cons: Can cause unnecessary re-renders of components that depend on memoized values. * **Inline filter**: + Pros: Simple and easy to understand. + Cons: May not be as efficient as the other two approaches, especially for large datasets. **Other considerations** When working with React, it's essential to consider the following: * **Optimization**: React components should strive to minimize unnecessary re-renders. This can often be achieved by using memoized values or optimizing dependencies. * **Performance**: The benchmark is running on a desktop browser (Chrome 114) and measuring executions per second. It's crucial to test performance in different environments, such as mobile devices and older browsers. **Library used** In this benchmark, React is used as the library for creating components and managing state. `React.createElement` is used to create virtual DOM elements, which are then rendered by `ReactDOM.render`. **Special JS feature or syntax** The benchmark uses a few modern JavaScript features: * **Arrow functions**: Used in the `ComponentUseCallback` and `ComponentUseMemo` functions. * **Template literals**: Used in the `KEYWORDS` variable. Overall, this benchmark provides a useful insight into the performance differences between three common React optimization techniques.
Related benchmarks:
filter.map vs reduce 2
filter vs some
filter vs some vs includes
Array.prototype.filter vs Lodash without 4
for loop filter vs native array.filter
Comments
Confirm delete:
Do you really want to delete benchmark?