mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-07-26 08:57:21 +02:00
Merge pull request #20997 from Snuffleupagus/StatTimer-Map
Re-factor the `StatTimer` class a little bit
This commit is contained in:
commit
80d0d7349c
@ -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("");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user