cloneElement vs child function

JavaScript performance comparison

Test case created by Andreas Eldh

Preparation code

<script src="https://unpkg.com/react@15.4.2/dist/react.min.js"></script>
<script src="https://unpkg.com/react-dom@15.4.2/dist/react-dom.min.js"></script>

<div id='clone'></div>
<div id='func'></div>


      
<script>
Benchmark.prototype.setup = function() {
  const {React} = window
  
  function Cloner({children}) {
    return React.createElement(
      'div',
      {className: 'foo'},
      React.Children.map(children, c => React.cloneElement(c, {className: 'bar'})),
    )
  }
  
  function Funcer({children}) {
    return React.createElement('div', {className: 'foo'}, children({className: 'bar'}))
  }
  
  function Clone() {
    return React.createElement(Cloner, {}, [
      React.createElement('div', {key: '1', className: 'foo'}, 'bar'),
      React.createElement('div', {key: '2', className: 'foo'}, 'bar'),
      React.createElement('div', {key: '3', className: 'foo'}, 'bar'),
      React.createElement('div', {key: '4', className: 'foo'}, 'bar'),
      React.createElement('div', {key: '5', className: 'foo'}, 'bar'),
      React.createElement('div', {key: '6', className: 'foo'}, 'bar'),
      React.createElement('div', {key: '7', className: 'foo'}, 'bar'),
    ])
  }
  
  function Func() {
    return React.createElement(Funcer, {}, ({className}) => {
      return [
        React.createElement('div', {key: '1', className: className + ' foo'}, 'bar'),
        React.createElement('div', {key: '2', className: className + ' foo'}, 'bar'),
        React.createElement('div', {key: '3', className: className + ' foo'}, 'bar'),
        React.createElement('div', {key: '4', className: className + ' foo'}, 'bar'),
        React.createElement('div', {key: '5', className: className + ' foo'}, 'bar'),
        React.createElement('div', {key: '6', className: className + ' foo'}, 'bar'),
        React.createElement('div', {key: '7', className: className + ' foo'}, 'bar'),
      ]
    })
  }
  
  function go(Component, id) {
  ReactDOM.render(React.createElement(Component), document.getElementById(id))
  }

};
</script>

Preparation code output

<script src="https://unpkg.com/react-dom@15.4.2/dist/react-dom.min.js"></script> <div id='clone'></div> <div id='func'></div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Func
go(Func, 'func')
pending…
Clone
go(Clone, 'clone')
pending…

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments