1010// This module can be loaded in an amd and commonjs-context.
1111// Because we want both instances to use the same perf-data
1212// we store them globally
13+ // stores data as 'type','name','startTime','duration'
1314global . _performanceEntries = global . _performanceEntries || [ ] ;
1415
1516if ( typeof define !== "function" && typeof module === "object" && typeof module . exports === "object" ) {
@@ -25,42 +26,35 @@ define([], function () {
2526 // const _now = global.performance && performance.now ? performance.now : Date.now
2627 const _now = Date . now ;
2728
28- class PerformanceEntry {
29- constructor ( type , name , startTime , duration ) {
30- this . type = type ;
31- this . name = name ;
32- this . startTime = startTime ;
33- this . duration = duration ;
34- }
29+ function importEntries ( entries ) {
30+ global . _performanceEntries . splice ( 0 , 0 , ...entries ) ;
3531 }
3632
37- function _getEntry ( type , name ) {
38- for ( let i = global . _performanceEntries . length - 1 ; i >= 0 ; i -- ) {
39- if (
40- ( type === undefined || global . _performanceEntries [ i ] . type === type ) &&
41- ( name === undefined || global . _performanceEntries [ i ] . name === name )
42- ) {
43- return global . _performanceEntries [ i ] ;
44- }
45- }
33+ function exportEntries ( ) {
34+ return global . _performanceEntries . splice ( 0 ) ;
4635 }
4736
48- function importEntries ( entries ) {
49- global . _performanceEntries . splice ( 0 , 0 , ...entries ) ;
50- }
37+ function getEntries ( type ) {
38+ const result = [ ] ;
39+ const entries = global . _performanceEntries ;
40+ for ( let i = 0 ; i < entries . length ; i += 4 ) {
41+ if ( entries [ i ] === type ) {
42+ result . push ( {
43+ type : entries [ i ] ,
44+ name : entries [ i + 1 ] ,
45+ startTime : entries [ i + 2 ] ,
46+ duration : entries [ i + 3 ] ,
47+ } ) ;
48+ }
49+ }
5150
52- function getEntries ( type , name ) {
53- return global . _performanceEntries . filter ( entry => {
54- return ( type === undefined || entry . type === type ) &&
55- ( name === undefined || entry . name === name ) ;
56- } ) . sort ( ( a , b ) => {
51+ return result . sort ( ( a , b ) => {
5752 return a . startTime - b . startTime ;
5853 } ) ;
5954 }
6055
6156 function mark ( name ) {
62- const entry = new PerformanceEntry ( 'mark' , name , _now ( ) , 0 ) ;
63- global . _performanceEntries . push ( entry ) ;
57+ global . _performanceEntries . push ( 'mark' , name , _now ( ) , 0 ) ;
6458 if ( typeof console . timeStamp === 'function' ) {
6559 console . timeStamp ( name ) ;
6660 }
@@ -81,25 +75,36 @@ define([], function () {
8175 if ( ! from ) {
8276 startTime = now ;
8377 } else {
84- startTime = _getEntry ( undefined , from ) . startTime ;
78+ startTime = _getLastStartTime ( from ) ;
8579 }
8680
8781 if ( ! to ) {
8882 duration = now - startTime ;
8983 } else {
90- duration = _getEntry ( undefined , to ) . startTime - startTime ;
84+ duration = _getLastStartTime ( to ) - startTime ;
85+ }
86+
87+ global . _performanceEntries . push ( 'measure' , name , startTime , duration ) ;
88+ }
89+
90+ function _getLastStartTime ( name ) {
91+ const entries = global . _performanceEntries ;
92+ for ( let i = entries . length - 1 ; i >= 0 ; i -= 4 ) {
93+ if ( entries [ i - 2 ] === name ) {
94+ return entries [ i - 1 ] ;
95+ }
9196 }
9297
93- const entry = new PerformanceEntry ( 'measure' , name , startTime , duration ) ;
94- global . _performanceEntries . push ( entry ) ;
98+ throw new Error ( name + ' not found' ) ;
9599 }
96100
97101 var exports = {
98102 mark : mark ,
99103 measure : measure ,
100104 time : time ,
101105 getEntries : getEntries ,
102- importEntries : importEntries
106+ importEntries : importEntries ,
107+ exportEntries : exportEntries
103108 } ;
104109
105110 return exports ;
0 commit comments