Merge pull request #20997 from Snuffleupagus/StatTimer-Map

Re-factor the `StatTimer` class a little bit
This commit is contained in:
Tim van der Meij 2026-03-29 16:05:15 +02:00 committed by GitHub
commit 80d0d7349c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -417,41 +417,37 @@ function getPdfFilenameFromUrl(url, defaultFilename = "document.pdf") {
} }
class StatTimer { class StatTimer {
started = Object.create(null); #started = new Map();
times = []; times = [];
time(name) { time(name) {
if (name in this.started) { if (this.#started.has(name)) {
warn(`Timer is already running for ${name}`); warn(`Timer is already running for ${name}`);
} }
this.started[name] = Date.now(); this.#started.set(name, Date.now());
} }
timeEnd(name) { timeEnd(name) {
if (!(name in this.started)) { if (!this.#started.has(name)) {
warn(`Timer has not been started for ${name}`); warn(`Timer has not been started for ${name}`);
} }
this.times.push({ this.times.push({
name, name,
start: this.started[name], start: this.#started.get(name),
end: Date.now(), end: Date.now(),
}); });
// Remove timer from started so it can be called again. // Remove timer from started so it can be called again.
delete this.started[name]; this.#started.delete(name);
} }
toString() { toString() {
// Find the longest name for padding purposes. // Find the longest name for padding purposes.
const outBuf = []; const longest = Math.max(...this.times.map(t => t.name.length));
let longest = 0;
for (const { name } of this.times) { return this.times
longest = Math.max(name.length, longest); .map(t => `${t.name.padEnd(longest)} ${t.end - t.start}ms\n`)
} .join("");
for (const { name, start, end } of this.times) {
outBuf.push(`${name.padEnd(longest)} ${end - start}ms\n`);
}
return outBuf.join("");
} }
} }