[perf] Change how ReactDefaultPerf is injected
ReactDefaultPerf should inject itself when require()'d. This continues to support the ?react_perf use case for logging on initial page load.
Pete Hunt committed
Feb 12, 2014 at 11:36 UTC
a6749a686f79c7732467aa0e80d1837aecb222ae
3 files changed
+29
-13
src/browser/ReactDefaultInjection.js
+8
-4
@@ -91,10 +91,6 @@ function inject() {
91
92
ReactInjection.DOMProperty.injectDOMPropertyConfig(DefaultDOMPropertyConfig);
93
94
- if (__DEV__) {
95
- ReactInjection.Perf.injectMeasure(require('ReactDefaultPerf').measure);
96
- }
97
-
94
ReactInjection.Updates.injectBatchingStrategy(
95
ReactDefaultBatchingStrategy
96
);
@@ -104,6 +100,14 @@ function inject() {
100
ClientReactRootIndex.createReactRootIndex :
101
ServerReactRootIndex.createReactRootIndex
102
);
103
+
104
+ if (__DEV__) {
105
+ var url = (ExecutionEnvironment.canUseDOM && window.location.href) || '';
106
+ if ((/[?&]react_perf\b/).test(url)) {
107
+ var ReactDefaultPerf = require('ReactDefaultPerf');
108
+ ReactDefaultPerf.start();
109
+ }
110
+ }
111
}
112
113
module.exports = {
src/test/ReactDefaultPerf.js
+16
-1
@@ -19,12 +19,27 @@
19
20
"use strict";
21
22
+var ReactPerf = require('ReactPerf');
23
+
24
var performanceNow = require('performanceNow');
25
26
var ReactDefaultPerf = {};
27
28
if (__DEV__) {
29
ReactDefaultPerf = {
30
+ _injected: false,
31
+
32
+ start: function() {
33
+ if (!ReactDefaultPerf._injected) {
34
+ ReactPerf.injection.injectMeasure(ReactDefaultPerf.measure);
35
+ }
36
+ ReactPerf.enableMeasure = true;
37
+ },
38
+
39
+ stop: function() {
40
+ ReactPerf.enableMeasure = false;
41
+ },
42
+
43
/**
44
* Gets the stored information for a given object's function.
45
*
@@ -293,7 +308,7 @@ if (__DEV__) {
308
var STRIP_COMMENTS = /((\/\/.*$)|(\/\*[\s\S]*?\*\/))/mg;
309
var fnStr = fn.toString().replace(STRIP_COMMENTS, '');
310
fnStr = fnStr.slice(fnStr.indexOf('(') + 1, fnStr.indexOf(')'));
296
- return fnStr.match(/([^\s,]+)/g);
311
+ return fnStr.match(/([^\s,]+)/g) || [];
312
};
313
314
/**
src/test/ReactPerf.js
+5
-8
@@ -19,6 +19,10 @@
19
20
"use strict";
21
22
+/**
23
+ * ReactPerf is a general AOP system designed to measure performance. This
24
+ * module only has the hooks: see ReactDefaultPerf for the analysis tool.
25
+ */
26
var ReactPerf = {
27
/**
28
* Boolean to enable/disable measurement. Set to false by default to prevent
@@ -33,7 +37,7 @@ var ReactPerf = {
37
storedMeasure: _noMeasure,
38
39
/**
36
- * Use this to wrap methods you want to measure.
40
+ * Use this to wrap methods you want to measure. Zero overhead in production.
41
*
42
* @param {string} objName
43
* @param {string} fnName
@@ -66,13 +70,6 @@ var ReactPerf = {
70
}
71
};
72
69
-if (__DEV__) {
70
- var ExecutionEnvironment = require('ExecutionEnvironment');
71
- var url = (ExecutionEnvironment.canUseDOM && window.location.href) || '';
72
- ReactPerf.enableMeasure = ReactPerf.enableMeasure ||
73
- (/[?&]react_perf\b/).test(url);
74
-}
75
-
73
/**
74
* Simply passes through the measured function, without measuring it.
75
*