React createElement vs cloneElement

Can anyone let me know if using cloneElement (on exist element instance) or createElement (on react Element class) which one is better in term of performance?

Sometimes cloning something is faster than create new instance.
Please let me know. Thanks

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

Using cloneElement will be usually be faster because you only need to instantiate one initial component.

This jsperf test shows cloneElement to be nearly twice as fast as createElement for Chromium 45 on Linux:

  • cloneElement ~1.7m ops/second
  • createElement ~0.85m ops/second

If you have a base component that you can clone without changing, then using cloneElement is a clear choice, both semantically and in terms of performance.

Method 2

In Safari 10.0.3, cloneElement is 31% slower for the JSPerf test mentioned above (in Chrome, createElement is 17% slower for me).

I also made another JSPerf test, where cloneElement is compared to “function-as-children”. In this case, cloneElement is slower by a big margin in both Chrome (26%) and Safari (46%).


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x