After upgrading to 15.1.0 I'm having trouble using the React Perf tools. I've done the following:
Added to one of my files:
import Perf from 'react-addons-perf';
window.Perf = Perf;
I then launch my app in Chrome. Open the Dev Tools and run this in the console:
Perf.start();
Perf.stop();
Perf.printWasted();
And I get:
TypeError: Cannot read property 'forEach' of undefined
getWasted @ 7.7.js:149515
printWasted @ 7.7.js:149666
(anonymous function) @ VM508:1
The failure is on this line: flushHistory.forEach(function (flush) {.
The same behaviour occurs for all other print* method such as printInclusive() and printExclusive().
The same behaviour occurs if I put the commands into my code (ie. NOT running in the Chrome Console).
This probably happens because you鈥檙e running a production build of React, but ReactPerf only works in the development build.
We need to make it clear by emitting a warning instead of crashing.
Ah. Yes, I am indeed running Perf in production mode as I wanted to avoid PropType validation getting in the way of profiling.
We鈥檒l allow this in the future, but it isn鈥檛 possible at the moment. You can track #6627.
If someone wants to take this, here鈥檚 what I鈥檇 do:
ReactDebugTool.getFlushHistory() return flushHistory regardless of the environmentReactPerf when __DEV__ is false@gaearon Hi, I will try to fix this!
Dang, just beat me to it
@sashashakun Thanks! I put myself as assignee so I don鈥檛 forget to come back to it, but please work on it and feel free to ask any questions!
@gaearon Great! Issue looks not difficult, but I want to do my best, so already started to work and because I don't have deep knowledge about React internals, have some questions:
About this point:
Add warnings and early returns to all methods in ReactPerf when DEV is false
1) I found 12 not deprecated methods in ReactPerf, so if I will simple add thing like
if (!__DEV__) {
warning('some warning message here');
return false;
}
at the beginning of each method it will be to much copy-paste, no? May be it will be better to make a separate helper, like a roundFloat in the beginning of ReactPerf.js?
2) What warning message I should use?
3) What need to return, false or may be 0?
4) One of the methods of ReactPerf use ReactDebugTool.getFlushHistory(), which i fixed this way
Make ReactDebugTool.getFlushHistory() return flushHistory regardless of the environment, so should I add __DEV__ checking from your first point to this method also?
May be it will be better to make a separate helper, like a roundFloat in the beginning of ReactPerf.js?
Sure, sounds good.
2) What warning message I should use?
ReactPerf is not supported in the production builds of React. To collect measurements, please use the development build of React instead.
3) What need to return, false or may be 0?
I think print*() methods can just exit early without a return value (just like they usually do anyway). get*() methods can probably return empty arrays. I suggest using whatever return type they usually have, but without any information.
so should I add DEV checking from your first point to this method also?
Worth adding it to log the warning, but the return value should be an empty array anyway.
Finally: I think we should make sure we don鈥檛 warn more than once. If somebody has ReactPerf.start() and ReactPerf.stop() calls around some hot function we don鈥檛 want to spam the console in production.
Start my work in branch react-perf-warnings-changes:
Sure, sounds good.
Done at ReactPerf.js#L23.
ReactPerf is not supported in the production builds of React. To collect measurements, please use the development build of React instead.
Added at ReactPerf.js#L26.
I suggest using whatever return type they usually have, but without any information.
Added at ReactPerf.js#L23. Value for return passed as param;
Worth adding it to log the warning, but the return value should be an empty array anyway
Added at ReactPerf.js#L34
Finally: I think we should make sure we don鈥檛 warn more than once.
Added variable 'alreadyWarned' which set turn off warning after first one and rollback after calling ReactPerf.stop()
Make ReactDebugTool.getFlushHistory() return flushHistory regardless of the environment
Done at ReactDebugTool.js#L210
Also, about steps before open PR:
master.grunt test).grunt lint) - we've done our best to make sure these rules match our internal linting guidelines.Looking forward for your feedback.
I suggest let鈥檚 open a PR now so I don鈥檛 forget to look into it?
Thanks a bunch!
Note that warning() is a noop in prod builds.
Haha, oops, I didn鈥檛 realize that 馃槥 .
Do you have any preferences about how to tackle this?
Let's just do a console.error.
Ok, will fix this