Skip to content

Instantly share code, notes, and snippets.

@mziemer21
Forked from saifelse/ReactPerfTester.js
Created September 12, 2016 08:05
Show Gist options
  • Select an option

  • Save mziemer21/942e7a736c1f7c6ef38f6e316dbb026f to your computer and use it in GitHub Desktop.

Select an option

Save mziemer21/942e7a736c1f7c6ef38f6e316dbb026f to your computer and use it in GitHub Desktop.

Revisions

  1. @saifelse saifelse created this gist Aug 25, 2016.
    34 changes: 34 additions & 0 deletions ReactPerfTester.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    /* eslint-disable no-console */
    import Perf from 'react-addons-perf';
    import sinon from 'sinon';

    /**
    * Counts the number of DOM operations that occurred while executing `fn`.
    *
    * Example usage:
    # import React from 'react';
    # import ReactDOM from 'react-dom';
    # const node = document.createElement('div');
    # const component = ReactDOM.render(<div><span>abc</span><span>def</span></div>, node);
    # const opsCount = getDOMOpsCount(() => {
    # ReactDOM.render(<div><span>ghi</span><span>jkl</span></div>, node);
    # });
    * console.log(opsCount); // 2
    *
    * @param {Function} fn function that renders a component into a DOM node with React
    * @return {Number} number of DOM operations
    */
    export function getDOMOpsCount(fn) {
    const oldTable = console.table;
    console.table = sinon.stub();
    try {
    Perf.start();
    fn();
    Perf.stop();
    Perf.printDOM();
    sinon.assert.calledOnce(console.table); // printDOM calls `console.table(results)`
    return console.table.getCall(0).args[0].length;
    } finally {
    console.table = oldTable;
    }
    }