mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-04-18 19:24:04 +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 {
|
||||
started = Object.create(null);
|
||||
#started = new Map();
|
||||
|
||||
times = [];
|
||||
|
||||
time(name) {
|
||||
if (name in this.started) {
|
||||
if (this.#started.has(name)) {
|
||||
warn(`Timer is already running for ${name}`);
|
||||
}
|
||||
this.started[name] = Date.now();
|
||||
this.#started.set(name, Date.now());
|
||||
}
|
||||
|
||||
timeEnd(name) {
|
||||
if (!(name in this.started)) {
|
||||
if (!this.#started.has(name)) {
|
||||
warn(`Timer has not been started for ${name}`);
|
||||
}
|
||||
this.times.push({
|
||||
name,
|
||||
start: this.started[name],
|
||||
start: this.#started.get(name),
|
||||
end: Date.now(),
|
||||
});
|
||||
// Remove timer from started so it can be called again.
|
||||
delete this.started[name];
|
||||
this.#started.delete(name);
|
||||
}
|
||||
|
||||
toString() {
|
||||
// Find the longest name for padding purposes.
|
||||
const outBuf = [];
|
||||
let longest = 0;
|
||||
for (const { name } of this.times) {
|
||||
longest = Math.max(name.length, longest);
|
||||
}
|
||||
for (const { name, start, end } of this.times) {
|
||||
outBuf.push(`${name.padEnd(longest)} ${end - start}ms\n`);
|
||||
}
|
||||
return outBuf.join("");
|
||||
const longest = Math.max(...this.times.map(t => t.name.length));
|
||||
|
||||
return this.times
|
||||
.map(t => `${t.name.padEnd(longest)} ${t.end - t.start}ms\n`)
|
||||
.join("");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user