Skip to content

Commit 80d0d73

Browse files
Merge pull request #20997 from Snuffleupagus/StatTimer-Map
Re-factor the `StatTimer` class a little bit
2 parents 37f5902 + 256be7f commit 80d0d73

File tree

1 file changed

+11
-15
lines changed

1 file changed

+11
-15
lines changed

src/display/display_utils.js

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -417,41 +417,37 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
417417
}
418418

419419
class StatTimer {
420-
started = Object.create(null);
420+
#started = new Map();
421421

422422
times = [];
423423

424424
time(name) {
425-
if (name in this.started) {
425+
if (this.#started.has(name)) {
426426
warn(`Timer is already running for ${name}`);
427427
}
428-
this.started[name] = Date.now();
428+
this.#started.set(name, Date.now());
429429
}
430430

431431
timeEnd(name) {
432-
if (!(name in this.started)) {
432+
if (!this.#started.has(name)) {
433433
warn(`Timer has not been started for ${name}`);
434434
}
435435
this.times.push({
436436
name,
437-
start: this.started[name],
437+
start: this.#started.get(name),
438438
end: Date.now(),
439439
});
440440
// Remove timer from started so it can be called again.
441-
delete this.started[name];
441+
this.#started.delete(name);
442442
}
443443

444444
toString() {
445445
// Find the longest name for padding purposes.
446-
const outBuf = [];
447-
let longest = 0;
448-
for (const { name } of this.times) {
449-
longest = Math.max(name.length, longest);
450-
}
451-
for (const { name, start, end } of this.times) {
452-
outBuf.push(`${name.padEnd(longest)} ${end - start}ms\n`);
453-
}
454-
return outBuf.join("");
446+
const longest = Math.max(...this.times.map(t => t.name.length));
447+
448+
return this.times
449+
.map(t => `${t.name.padEnd(longest)} ${t.end - t.start}ms\n`)
450+
.join("");
455451
}
456452
}
457453

0 commit comments

Comments
 (0)