2 * Augment mw.loader to facilitate module-level profiling.
11 var moduleTimes
= Object
.create( null );
14 * Private hooks inserted into mw.loader code if MediaWiki configuration
15 * `$wgResourceLoaderEnableJSProfiler` is `true`.
17 * To use this data, run `mw.inspect( 'time' )` from the browser console.
24 mw
.loader
.profiler
= {
25 onExecuteStart: function ( moduleName
) {
26 var time
= performance
.now();
27 if ( moduleTimes
[ moduleName
] ) {
28 throw new Error( 'Unexpected perf record for "' + moduleName
+ '".' );
30 moduleTimes
[ moduleName
] = {
37 onExecuteEnd: function ( moduleName
) {
38 var time
= performance
.now();
39 moduleTimes
[ moduleName
].executeEnd
= time
;
41 onScriptStart: function ( moduleName
) {
42 var time
= performance
.now();
43 moduleTimes
[ moduleName
].scriptStart
= time
;
45 onScriptEnd: function ( moduleName
) {
46 var time
= performance
.now();
47 moduleTimes
[ moduleName
].scriptEnd
= time
;
51 * For internal use by inspect.reports#time.
54 * @param {string} moduleName
55 * @return {Object|null}
56 * @throws {Error} If the perf record is incomplete.
58 getProfile: function ( moduleName
) {
59 var times
, key
, execute
, script
, total
;
60 times
= moduleTimes
[ moduleName
];
64 for ( key
in times
) {
65 if ( times
[ key
] === null ) {
66 throw new Error( 'Incomplete perf record for "' + moduleName
+ '".', times
);
69 execute
= times
.executeEnd
- times
.executeStart
;
70 script
= times
.scriptEnd
- times
.scriptStart
;
71 total
= execute
+ script
;