diff --git a/src/renderers/shared/ReactDebugTool.js b/src/renderers/shared/ReactDebugTool.js index 97907beadc603..05c7213942bca 100644 --- a/src/renderers/shared/ReactDebugTool.js +++ b/src/renderers/shared/ReactDebugTool.js @@ -79,7 +79,7 @@ function resetMeasurements() { var previousMeasurements = currentFlushMeasurements || []; var previousOperations = ReactHostOperationHistoryDevtool.getHistory(); - if (!isProfiling || currentFlushNesting === 0) { + if (currentFlushNesting === 0) { currentFlushStartTime = null; currentFlushMeasurements = null; clearHistory(); @@ -106,7 +106,7 @@ function checkDebugID(debugID) { } function beginLifeCycleTimer(debugID, timerType) { - if (!isProfiling || currentFlushNesting === 0) { + if (currentFlushNesting === 0) { return; } warning( @@ -125,7 +125,7 @@ function beginLifeCycleTimer(debugID, timerType) { } function endLifeCycleTimer(debugID, timerType) { - if (!isProfiling || currentFlushNesting === 0) { + if (currentFlushNesting === 0) { return; } warning( @@ -137,11 +137,13 @@ function endLifeCycleTimer(debugID, timerType) { currentTimerType || 'no', (debugID === currentTimerDebugID) ? 'the same' : 'another' ); - currentFlushMeasurements.push({ - timerType, - instanceID: debugID, - duration: performanceNow() - currentTimerStartTime - currentTimerNestedFlushDuration, - }); + if (isProfiling) { + currentFlushMeasurements.push({ + timerType, + instanceID: debugID, + duration: performanceNow() - currentTimerStartTime - currentTimerNestedFlushDuration, + }); + } currentTimerStartTime = null; currentTimerNestedFlushDuration = null; currentTimerDebugID = null; diff --git a/src/renderers/shared/__tests__/ReactPerf-test.js b/src/renderers/shared/__tests__/ReactPerf-test.js index babd6a03a5125..d99b0691bc120 100644 --- a/src/renderers/shared/__tests__/ReactPerf-test.js +++ b/src/renderers/shared/__tests__/ReactPerf-test.js @@ -467,5 +467,49 @@ describe('ReactPerf', function() { expect(console.error.calls.count()).toBe(1); __DEV__ = true; - }) + }); + + it('should work when measurement starts during reconciliation', () => { + // https://github.com/facebook/react/issues/6949#issuecomment-230371009 + var Measurer = React.createClass({ + componentWillMount() { + ReactPerf.start(); + }, + componentDidMount() { + ReactPerf.stop(); + }, + componentWillUpdate() { + ReactPerf.start(); + }, + componentDidUpdate() { + ReactPerf.stop(); + }, + render() { + // Force reconciliation despite constant element + return React.cloneElement(this.props.children); + }, + }); + + var container = document.createElement('div'); + ReactDOM.render(, container); + expect(ReactPerf.getWasted()).toEqual([]); + + ReactDOM.render(, container); + expect(ReactPerf.getWasted()).toEqual([{ + key: 'Measurer', + instanceCount: 1, + inclusiveRenderDuration: 4, + renderCount: 1, + }, { + key: 'App', + instanceCount: 1, + inclusiveRenderDuration: 3, + renderCount: 1, + }, { + key: 'App > Box', + instanceCount: 2, + inclusiveRenderDuration: 2, + renderCount: 2, + }]); + }); });