Skip to content

Commit 36d68b1

Browse files
authored
Convert internal Benchmarker to ES6 class. NFC (#21543)
We don't have any users of this class but I verified that it works locally. I wonder if we should perhaps just delete this since the JS compiler no is not longer something that takes much time.
1 parent e74cf31 commit 36d68b1

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

.eslintrc.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ env:
55
extends:
66
- prettier
77
parserOptions:
8-
ecmaVersion: 12
8+
ecmaVersion: 13
99
ignorePatterns:
1010
- "out/"
1111
- "site/"

src/utility.js

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -232,32 +232,34 @@ function isPowerOfTwo(x) {
232232
return x > 0 && ((x & (x - 1)) == 0);
233233
}
234234

235-
/** @constructor */
236-
globalThis.Benchmarker = function() {
237-
const totals = {};
238-
const ids = [];
239-
const lastTime = 0;
240-
this.start = function(id) {
235+
class Benchmarker {
236+
totals = {};
237+
ids = [];
238+
lastTime = 0;
239+
240+
start(id) {
241241
const now = Date.now();
242-
if (ids.length > 0) {
243-
totals[ids[ids.length - 1]] += now - lastTime;
242+
if (this.ids.length > 0) {
243+
this.totals[this.ids[this.ids.length - 1]] += now - this.lastTime;
244244
}
245-
lastTime = now;
246-
ids.push(id);
247-
totals[id] ||= 0;
248-
};
249-
this.stop = function(id) {
245+
this.lastTime = now;
246+
this.ids.push(id);
247+
this.totals[id] ||= 0;
248+
}
249+
250+
stop(id) {
250251
const now = Date.now();
251-
assert(id === ids[ids.length - 1]);
252-
totals[id] += now - lastTime;
253-
lastTime = now;
254-
ids.pop();
255-
};
256-
this.print = function(text) {
257-
const ids = Object.keys(totals);
252+
assert(id === this.ids[this.ids.length - 1]);
253+
this.totals[id] += now - this.lastTime;
254+
this.lastTime = now;
255+
this.ids.pop();
256+
}
257+
258+
print(text) {
259+
const ids = Object.keys(this.totals);
258260
if (ids.length > 0) {
259-
ids.sort((a, b) => totals[b] - totals[a]);
260-
printErr(text + ' times: \n' + ids.map((id) => id + ' : ' + totals[id] + ' ms').join('\n'));
261+
ids.sort((a, b) => this.totals[b] - this.totals[a]);
262+
printErr(text + ' times: \n' + ids.map((id) => id + ' : ' + this.totals[id] + ' ms').join('\n'));
261263
}
262-
};
264+
}
263265
}

0 commit comments

Comments
 (0)