Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
React 1
(version: 1)
Comparing performance of:
Inline vs Reference
Created:
one year ago
by:
Guest
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:
const DEFAULT_PROPS = {c: 1, d: 1}; const clickMe = () => { console.log('hello world'); } function InlineTest(props) { const {a, b, c, d} = {...DEFAULT_PROPS, ...props} return React.createElement('button', {}, 'Click me!'); } function ReferenceTest(props) { // const {a, b, c, d} = React.useMemo() => ({...DEFAULT_PROPS, ...props}), [props]); return React.createElement('button', {}, 'Click me!'); }
Tests:
Inline
ReactDOM.render(React.createElement(InlineTest, { a: 1, b: 2}), document.getElementById('root'))
Reference
ReactDOM.render(React.createElement(ReferenceTest, { a: 1, b: 2}), document.getElementById('root'))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Inline
Reference
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36
Browser/OS:
Chrome 131 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Inline
934710.4 Ops/sec
Reference
947936.4 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark is designed to evaluate the performance differences between two React component rendering approaches: **InlineTest** and **ReferenceTest**. Both components ultimately render a button, but they handle props in slightly different ways. ### Options Compared 1. **InlineTest**: - Utilizes a straightforward destructuring of props with default values. - The component returns a button element created with `React.createElement`. 2. **ReferenceTest**: - Commented-out code suggests a use of `React.useMemo()`, which, if enabled, would memoize the props and only recompute when the props change, potentially optimizing performance by avoiding unnecessary recalculations. - However, the current implementation of `ReferenceTest` does not utilize `useMemo`, making its functionality similar to `InlineTest`. ### Pros and Cons - **InlineTest**: - **Pros**: - Simple and straightforward. Easy to understand and implement. - Every render uses the fresh props, which might be adequate for performance based on how often props change. - **Cons**: - Every render creates new instances of objects, which can lead to performance overhead when there are many re-renders. - **ReferenceTest**: - **Pros** (if `useMemo` were applied): - Performance optimization by storing the input values and preventing unnecessary renders. - Efficient when the same props are passed multiple times without changes. - **Cons**: - If `useMemo` is not implemented (as seen in the benchmark), it does not provide any advantage over InlineTest. - Adding complexity that may not be necessary for simpler components or infrequent updates. ### Libraries The benchmark employs **React** and **ReactDOM** from a CDN. React is a JavaScript library for building user interfaces, especially single-page applications, and provides features for building component-based interfaces. ReactDOM is a package that provides DOM-specific methods that can be used at the top level of an app to enable an efficient way to interact with the DOM from React components. ### JavaScript Features and Considerations The code primarily uses standard JavaScript functions and ES6 syntax (like arrow functions, destructuring assignment, and the spread operator). No special JavaScript features beyond regular functions and the React API are exploited. ### Alternatives - Instead of using `React.createElement`, one could use JSX, which is more syntactically appealing and produces more readable code: ```jsx function InlineTest(props) { const { a, b, c, d } = { ...DEFAULT_PROPS, ...props }; return <button>Click me!</button>; } ``` - Another alternative could be using other libraries like Vue.js or Angular for component rendering, depending on the project requirements. Each of these frameworks/libraries has its approach to handling updates and efficiency but may offer different performance characteristics based on the complexity and structure of components. ### Conclusions The benchmark reveals not only the raw performance metrics but also insights on best practices for component optimization in React. If `ReferenceTest` were fully implemented with `useMemo`, it would likely demonstrate substantial benefits in scenarios with frequent renders and static props. It's crucial to select the right rendering technique based on the specific use case to optimize performance effectively.
Related benchmarks:
React Hooks 3
React Spreading Props vs Seperate Props
React useCallback
React
useMemo
With or without useMemo
Test UseRef
React 2
React 3
Comments
Confirm delete:
Do you really want to delete benchmark?