Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81152 views
1
/**
2
* Copyright 2013-2014, Facebook, Inc.
3
* All rights reserved.
4
*
5
* This source code is licensed under the BSD-style license found in the
6
* LICENSE file in the root directory of this source tree. An additional grant
7
* of patent rights can be found in the PATENTS file in the same directory.
8
*
9
* @providesModule ReactPerf
10
* @typechecks static-only
11
*/
12
13
"use strict";
14
15
/**
16
* ReactPerf is a general AOP system designed to measure performance. This
17
* module only has the hooks: see ReactDefaultPerf for the analysis tool.
18
*/
19
var ReactPerf = {
20
/**
21
* Boolean to enable/disable measurement. Set to false by default to prevent
22
* accidental logging and perf loss.
23
*/
24
enableMeasure: false,
25
26
/**
27
* Holds onto the measure function in use. By default, don't measure
28
* anything, but we'll override this if we inject a measure function.
29
*/
30
storedMeasure: _noMeasure,
31
32
/**
33
* Use this to wrap methods you want to measure. Zero overhead in production.
34
*
35
* @param {string} objName
36
* @param {string} fnName
37
* @param {function} func
38
* @return {function}
39
*/
40
measure: function(objName, fnName, func) {
41
if (__DEV__) {
42
var measuredFunc = null;
43
var wrapper = function() {
44
if (ReactPerf.enableMeasure) {
45
if (!measuredFunc) {
46
measuredFunc = ReactPerf.storedMeasure(objName, fnName, func);
47
}
48
return measuredFunc.apply(this, arguments);
49
}
50
return func.apply(this, arguments);
51
};
52
wrapper.displayName = objName + '_' + fnName;
53
return wrapper;
54
}
55
return func;
56
},
57
58
injection: {
59
/**
60
* @param {function} measure
61
*/
62
injectMeasure: function(measure) {
63
ReactPerf.storedMeasure = measure;
64
}
65
}
66
};
67
68
/**
69
* Simply passes through the measured function, without measuring it.
70
*
71
* @param {string} objName
72
* @param {string} fnName
73
* @param {function} func
74
* @return {function}
75
*/
76
function _noMeasure(objName, fnName, func) {
77
return func;
78
}
79
80
module.exports = ReactPerf;
81
82