Skip to content

Commit 8d15b4e

Browse files
author
Robert Jackson
committed
Add counters for better insight into frequency...
Adds simple lightweight counters to each of the public API methods, event counts, etc. The idea here is that we can begin getting better insight into which operations are the most common in _real_ applications. This will help us know which things are more important from a performance perspective...
1 parent 40e01c7 commit 8d15b4e

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

lib/index.ts

+76
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,28 @@ function parseArgs() {
4545

4646
let UUID = 0;
4747

48+
let beginCount = 0;
49+
let endCount = 0;
50+
let beginEventCount = 0;
51+
let endEventCount = 0;
52+
let runCount = 0;
53+
let joinCount = 0;
54+
let deferCount = 0;
55+
let scheduleCount = 0;
56+
let scheduleIterableCount = 0;
57+
let deferOnceCount = 0;
58+
let scheduleOnceCount = 0;
59+
let setTimeoutCount = 0;
60+
let laterCount = 0;
61+
let throttleCount = 0;
62+
let debounceCount = 0;
63+
let cancelTimersCount = 0;
64+
let cancelCount = 0;
65+
let autorunsCreatedCount = 0;
66+
let autorunsCompletedCount = 0;
67+
let deferredActionQueuesCreatedCount = 0;
68+
let nestedDeferredActionQueuesCreated = 0;
69+
4870
export default class Backburner {
4971
public static Queue = Queue;
5072

@@ -54,6 +76,38 @@ export default class Backburner {
5476

5577
public options: any;
5678

79+
public get counters() {
80+
return {
81+
begin: beginCount,
82+
end: endCount,
83+
events: {
84+
begin: beginEventCount,
85+
end: endEventCount,
86+
},
87+
autoruns: {
88+
created: autorunsCreatedCount,
89+
completed: autorunsCompletedCount,
90+
},
91+
run: runCount,
92+
join: joinCount,
93+
defer: deferCount,
94+
schedule: scheduleCount,
95+
scheduleIterable: scheduleIterableCount,
96+
deferOnce: deferOnceCount,
97+
scheduleOnce: scheduleOnceCount,
98+
setTimeout: setTimeoutCount,
99+
later: laterCount,
100+
throttle: throttleCount,
101+
debounce: debounceCount,
102+
cancelTimers: cancelTimersCount,
103+
cancel: cancelCount,
104+
loops: {
105+
total: deferredActionQueuesCreatedCount,
106+
nested: nestedDeferredActionQueuesCreated,
107+
},
108+
};
109+
}
110+
57111
private _onBegin: (currentInstance: DeferredActionQueues, previousInstance: DeferredActionQueues | null) => void;
58112
private _onEnd: (currentInstance: DeferredActionQueues, nextInstance: DeferredActionQueues | null) => void;
59113
private queueNames: string[];
@@ -109,6 +163,7 @@ export default class Backburner {
109163
};
110164

111165
this._boundAutorunEnd = () => {
166+
autorunsCompletedCount++;
112167
this._autorun = null;
113168
this.end();
114169
};
@@ -119,6 +174,7 @@ export default class Backburner {
119174
@return instantiated class DeferredActionQueues
120175
*/
121176
public begin(): DeferredActionQueues {
177+
beginCount++;
122178
let options = this.options;
123179
let previousInstance = this.currentInstance;
124180
let current;
@@ -128,9 +184,12 @@ export default class Backburner {
128184
this._cancelAutorun();
129185
} else {
130186
if (previousInstance !== null) {
187+
nestedDeferredActionQueuesCreated++;
131188
this.instanceStack.push(previousInstance);
132189
}
190+
deferredActionQueuesCreatedCount++;
133191
current = this.currentInstance = new DeferredActionQueues(this.queueNames, options);
192+
beginEventCount++;
134193
this._trigger('begin', current, previousInstance);
135194
}
136195

@@ -140,6 +199,7 @@ export default class Backburner {
140199
}
141200

142201
public end() {
202+
endCount++;
143203
let currentInstance = this.currentInstance;
144204
let nextInstance: DeferredActionQueues | null = null;
145205

@@ -158,6 +218,7 @@ export default class Backburner {
158218
finallyAlreadyCalled = true;
159219

160220
if (result === QUEUE_STATE.Pause) {
221+
autorunsCreatedCount++;
161222
const next = this._platform.next;
162223
this._autorun = next(this._boundAutorunEnd);
163224
} else {
@@ -167,6 +228,7 @@ export default class Backburner {
167228
nextInstance = this.instanceStack.pop() as DeferredActionQueues;
168229
this.currentInstance = nextInstance;
169230
}
231+
endEventCount++;
170232
this._trigger('end', currentInstance, nextInstance);
171233
this._onEnd(currentInstance, nextInstance);
172234
}
@@ -210,6 +272,7 @@ export default class Backburner {
210272
public run(target: Function | any | null, method?: Function | string, ...args);
211273
public run(target: any | null | undefined, method?: Function, ...args: any[]);
212274
public run() {
275+
runCount++;
213276
let [target, method, args] = parseArgs(...arguments);
214277
return this._run(target, method, args);
215278
}
@@ -232,6 +295,7 @@ export default class Backburner {
232295
public join(target: Function | any | null, method?: Function | string, ...args);
233296
public join(target: any | null | undefined, method?: Function, ...args: any[]);
234297
public join() {
298+
joinCount++;
235299
let [target, method, args] = parseArgs(...arguments);
236300
return this._join(target, method, args);
237301
}
@@ -240,6 +304,7 @@ export default class Backburner {
240304
* @deprecated please use schedule instead.
241305
*/
242306
public defer(queueName, targetOrMethod, ..._args) {
307+
deferCount++;
243308
return this.schedule(queueName, targetOrMethod, ..._args);
244309
}
245310

@@ -250,6 +315,7 @@ export default class Backburner {
250315
public schedule<T, U extends keyof T>(queueName: string, target: T, method: U, ...args);
251316
public schedule(queueName: string, target: any, method: any | Function, ...args);
252317
public schedule(queueName, ..._args) {
318+
scheduleCount++;
253319
let [target, method, args] = parseArgs(..._args);
254320
let stack = this.DEBUG ? new Error() : undefined;
255321
return this._ensureInstance().schedule(queueName, target, method, args, false, stack);
@@ -264,6 +330,7 @@ export default class Backburner {
264330
@return method result
265331
*/
266332
public scheduleIterable(queueName: string, iterable: () => Iteratable) {
333+
scheduleIterableCount++;
267334
let stack = this.DEBUG ? new Error() : undefined;
268335
return this._ensureInstance().schedule(queueName, null, iteratorDrain, [iterable], false, stack);
269336
}
@@ -272,6 +339,7 @@ export default class Backburner {
272339
* @deprecated please use scheduleOnce instead.
273340
*/
274341
public deferOnce(queueName, targetOrMethod, ...args) {
342+
deferOnceCount++;
275343
return this.scheduleOnce(queueName, targetOrMethod, ...args);
276344
}
277345

@@ -282,6 +350,7 @@ export default class Backburner {
282350
public scheduleOnce<T, U extends keyof T>(queueName: string, target: T, method: U, ...args);
283351
public scheduleOnce(queueName: string, target: any | null, method: any | Function, ...args);
284352
public scheduleOnce(queueName, ..._args) {
353+
scheduleOnceCount++;
285354
let [target, method, args] = parseArgs(..._args);
286355
let stack = this.DEBUG ? new Error() : undefined;
287356
return this._ensureInstance().schedule(queueName, target, method, args, true, stack);
@@ -292,10 +361,12 @@ export default class Backburner {
292361
*/
293362
public setTimeout(...args);
294363
public setTimeout() {
364+
setTimeoutCount++;
295365
return this.later(...arguments);
296366
}
297367

298368
public later(...args) {
369+
laterCount++;
299370
let length = args.length;
300371

301372
let wait = 0;
@@ -349,6 +420,7 @@ export default class Backburner {
349420
public throttle<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait?: number | string, immediate?: boolean): Timer;
350421
public throttle<A, B, C>(method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait?: number | string, immediate?: boolean): Timer;
351422
public throttle(targetOrThisArgOrMethod: object | Function, ...args): Timer {
423+
throttleCount++;
352424
let target;
353425
let method;
354426
let immediate;
@@ -425,6 +497,7 @@ export default class Backburner {
425497
public debounce<A, B>(method: (arg1: A, arg2: B) => void, arg1: A, arg2: B, wait: number | string, immediate?: boolean): Timer;
426498
public debounce<A, B, C>(method: (arg1: A, arg2: B, arg3: C) => void, arg1: A, arg2: B, arg3: C, wait: number | string, immediate?: boolean): Timer;
427499
public debounce(targetOrThisArgOrMethod: object | Function, ...args): Timer {
500+
debounceCount++;
428501
let target;
429502
let method;
430503
let immediate;
@@ -487,6 +560,7 @@ export default class Backburner {
487560
}
488561

489562
public cancelTimers() {
563+
cancelTimersCount++;
490564
for (let i = 3; i < this._throttlers.length; i += 4) {
491565
this._platform.clearTimeout(this._throttlers[i]);
492566
}
@@ -511,6 +585,7 @@ export default class Backburner {
511585
}
512586

513587
public cancel(timer?) {
588+
cancelCount++;
514589
if (!timer) { return false; }
515590
let timerType = typeof timer;
516591

@@ -693,6 +768,7 @@ export default class Backburner {
693768
private _ensureInstance(): DeferredActionQueues {
694769
let currentInstance = this.currentInstance;
695770
if (currentInstance === null) {
771+
autorunsCreatedCount++;
696772
currentInstance = this.begin();
697773
const next = this._platform.next;
698774
this._autorun = next(this._boundAutorunEnd);

0 commit comments

Comments
 (0)