React createElement vs cloneElement

白昼怎懂夜的黑 提交于 2019-12-05 06:46:55

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.

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%).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!