Skip to content

Commit

Permalink
Merge pull request #6287 from gaearon/rename-perf-methods
Browse files Browse the repository at this point in the history
Rename ReactPerf methods to match the upcoming ReactPerf revamp
  • Loading branch information
gaearon committed Mar 17, 2016
2 parents 8046cbd + ea55d66 commit 5520c39
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
6 changes: 5 additions & 1 deletion docs/docs/10.9-perf.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ Prints the overall time taken. If no argument's passed, defaults to all the meas

![](/react/img/docs/perf-wasted.png)

### `Perf.printDOM(measurements)`
### `Perf.printOperations(measurements)`
Prints the underlying DOM manipulations, e.g. "set innerHTML" and "remove".

![](/react/img/docs/perf-dom.png)

### `Perf.printDOM(measurements)`

This method has been renamed to `printOperations()` which is described in the previous paragraph. Currently `printDOM()` still exists as an alias but it prints a deprecation warning and will eventually be removed.

## Advanced API

The above print methods use `Perf.getLastMeasurements()` to pretty-print the result.
Expand Down
26 changes: 25 additions & 1 deletion src/test/ReactDefaultPerf.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var ReactMount = require('ReactMount');
var ReactPerf = require('ReactPerf');

var performanceNow = require('performanceNow');
var warning = require('warning');

function roundFloat(val) {
return Math.floor(val * 100) / 100;
Expand Down Expand Up @@ -62,6 +63,9 @@ function unwrapLegacyMeasurements(measurements) {
return measurements && measurements.__unstable_this_format_will_change || measurements;
}

var warnedAboutPrintDOM = false;
var warnedAboutGetMeasurementsSummaryMap = false;

var ReactDefaultPerf = {
_allMeasurements: [], // last item in the list is the current one
_mountStack: [0],
Expand Down Expand Up @@ -120,6 +124,16 @@ var ReactDefaultPerf = {
},

getMeasurementsSummaryMap: function(measurements) {
warning(
warnedAboutGetMeasurementsSummaryMap,
'`ReactPerf.getMeasurementsSummaryMap(...)` is deprecated. Use ' +
'`ReactPerf.getWasted(...)` instead.'
);
warnedAboutGetMeasurementsSummaryMap = true;
return ReactDefaultPerf.getWasted(measurements);
},

getWasted: function(measurements) {
measurements = unwrapLegacyMeasurements(measurements);
var summary = ReactDefaultPerfAnalysis.getInclusiveSummary(
measurements,
Expand All @@ -136,14 +150,24 @@ var ReactDefaultPerf = {

printWasted: function(measurements) {
measurements = unwrapLegacyMeasurements(measurements || ReactDefaultPerf._allMeasurements);
console.table(ReactDefaultPerf.getMeasurementsSummaryMap(measurements));
console.table(ReactDefaultPerf.getWasted(measurements));
console.log(
'Total time:',
ReactDefaultPerfAnalysis.getTotalTime(measurements).toFixed(2) + ' ms'
);
},

printDOM: function(measurements) {
warning(
warnedAboutPrintDOM,
'`ReactPerf.printDOM(...)` is deprecated. Use ' +
'`ReactPerf.printOperations(...)` instead.'
);
warnedAboutPrintDOM = true;
return ReactDefaultPerf.printOperations(measurements);
},

printOperations: function(measurements) {
measurements = unwrapLegacyMeasurements(measurements || ReactDefaultPerf._allMeasurements);
var summary = ReactDefaultPerfAnalysis.getDOMSummary(measurements);
console.table(summary.map(function(item) {
Expand Down
45 changes: 42 additions & 3 deletions src/test/__tests__/ReactDefaultPerf-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ describe('ReactDefaultPerf', function() {
return now++;
});

if (typeof console.table !== 'function') {
console.table = () => {};
console.table.isFake = true;
}

React = require('React');
ReactDOM = require('ReactDOM');
ReactDefaultPerf = require('ReactDefaultPerf');
Expand All @@ -54,6 +59,12 @@ describe('ReactDefaultPerf', function() {
});
});

afterEach(function() {
if (console.table.isFake) {
delete console.table;
}
});

function measure(fn) {
ReactDefaultPerf.start();
fn();
Expand All @@ -68,7 +79,7 @@ describe('ReactDefaultPerf', function() {
ReactDOM.render(<App />, container);
});

var summary = ReactDefaultPerf.getMeasurementsSummaryMap(measurements);
var summary = ReactDefaultPerf.getWasted(measurements);
expect(summary.length).toBe(2);

/*eslint-disable dot-notation */
Expand All @@ -94,7 +105,7 @@ describe('ReactDefaultPerf', function() {
ReactDOM.render(<App flipSecond={true} />, container);
});

var summary = ReactDefaultPerf.getMeasurementsSummaryMap(measurements);
var summary = ReactDefaultPerf.getWasted(measurements);
expect(summary.length).toBe(1);

/*eslint-disable dot-notation */
Expand All @@ -108,7 +119,7 @@ describe('ReactDefaultPerf', function() {

function expectNoWaste(fn) {
var measurements = measure(fn);
var summary = ReactDefaultPerf.getMeasurementsSummaryMap(measurements);
var summary = ReactDefaultPerf.getWasted(measurements);
expect(summary).toEqual([]);
}

Expand Down Expand Up @@ -226,4 +237,32 @@ describe('ReactDefaultPerf', function() {
expect(summary).toEqual([]);
});

it('warns once when using getMeasurementsSummaryMap', function() {
var measurements = measure(() => {});
spyOn(console, 'error');
ReactDefaultPerf.getMeasurementsSummaryMap(measurements);
expect(console.error.calls.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain(
'`ReactPerf.getMeasurementsSummaryMap(...)` is deprecated. Use ' +
'`ReactPerf.getWasted(...)` instead.'
);

ReactDefaultPerf.getMeasurementsSummaryMap(measurements);
expect(console.error.calls.length).toBe(1);
});

it('warns once when using printDOM', function() {
var measurements = measure(() => {});
spyOn(console, 'error');
ReactDefaultPerf.printDOM(measurements);
expect(console.error.calls.length).toBe(1);
expect(console.error.argsForCall[0][0]).toContain(
'`ReactPerf.printDOM(...)` is deprecated. Use ' +
'`ReactPerf.printOperations(...)` instead.'
);

ReactDefaultPerf.printDOM(measurements);
expect(console.error.calls.length).toBe(1);
});

});

0 comments on commit 5520c39

Please sign in to comment.