<script src="https://unpkg.com/react@18/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@18/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<div id="root"></div>
<script type="text/babel">
const container = document.getElementById('root');
const root = ReactDOM.createRoot(container);
</script>
const Path = React.createElement('path', {d: "M180-120q-24 0-42-18t-18-42v-600q0-24 18-42t42-18h279v60H180v600h600v-279h60v279q0 24-18 42t-42 18H180Zm202-219-42-43 398-398H519v-60h321v321h-60v-218L382-339Z"});
const SVG = React.createElement('svg', {}, Path);
root.render(React.createElement(Element, {width: 100}, SVG));
function Element({children}) {
return React.createElement('svg', {width: 24, height:24}, children)
}
root.render(React.createElement(Element, {width: 100}, Path))
function Element({children}) {
return element = React.cloneElement(children, {width: 24, height:24})
}
root.render(React.createElement(Element, {width: 100}, SVG))
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createElement | |
cloneElement |
Test name | Executions per second |
---|---|
createElement | 638590.5 Ops/sec |
cloneElement | 551773.3 Ops/sec |
The benchmark compares the performance of two different React methods for creating or rendering elements: React.createElement
and React.cloneElement
. Both methods serve to generate React elements but are used in different contexts, which allows us to evaluate their efficiency under the same conditions.
React.createElement
:
Element
component that returns a new SVG element with the specified children.function Element({children}) {
return React.createElement('svg', {width: 24, height: 24}, children);
}
React.cloneElement
:
Element
) and modifies its properties, specifically the width and height.function Element({children}) {
return React.cloneElement(children, {width: 24, height: 24});
}
The results show how many executions of each method were performed per second:
React.createElement
: 1,384,654.25 executions/secondReact.cloneElement
: 1,366,804.125 executions/secondWhile both methods exhibit high performance, createElement
performed slightly better in this benchmark.
React.createElement
Pros:
Cons:
React.cloneElement
Pros:
Cons:
Use Cases: The choice between these methods often comes down to the specific requirements of the components being built. In scenarios where new elements need to be generated dynamically, createElement
is the better approach. Conversely, if you need to modify an existing element's properties, cloneElement
is more appropriate.
Alternatives: Beyond these two methods, other strategies for managing components in React include:
This benchmark illustrates the trade-offs between React.createElement
and React.cloneElement
. While performance is crucial, the best choice depends not just on speed but also on code maintainability and readability. Understanding when to use each method is key for developing efficient React applications.