Class
Profiler
View sourceCollects named duration measurements as a world resource.
Use Profiler#start and Profiler#end around the
work to measure. Labels identify entries in Profiler#profiles; a
missing label is created by start, while end silently does nothing for
a label that has not been started. Measurements use performance.now() and
therefore report elapsed milliseconds rather than seconds.
The profiles map stores one Profile for each label, and the
static serialization methods preserve those entries as plain data.
Examples
import { Profiler } from '@wimaengine/profiler'
const profiler = new Profiler()
profiler.start('load-level')
// loadLevel()
profiler.end('load-level')
console.log(profiler.get('load-level')?.delta) // millisecondsConstructor
Properties
profiles: Map<string, Profile>
View sourceProfiles keyed by the labels passed to Profiler#start.
Calling Profiler#set for an existing label replaces its
profile. Map iteration order is retained by serialization.
Default: ...
Methods
set(label: string): Profile
View sourceCreates and stores a new profile for a label.
If the label already exists, its previous profile is replaced and the
returned profile starts with lastTick and delta set to 0.
get(label: string): Profile
View sourceGets the profile stored for a label.
start(label: string): void
View sourceStarts measuring a labeled interval.
An absent label is created. Existing profiles keep their previous
delta; this method only replaces lastTick with the current
performance.now() timestamp. Calling start again for the same label
restarts that label’s interval.
end(label: string): void
View sourceEnds a labeled interval and records its duration.
For an existing profile, delta becomes the current
performance.now() value minus lastTick, in milliseconds. If the label
is unknown, the method does nothing and does not create a profile.
staticserialize(value: Profiler): ProfilerSerial
View sourceConverts a profiler to a serializable plain object.
Profiles are emitted as [label, profile] pairs in map iteration order;
each profile value is converted with Profile.serialize.
The returned object and entries are newly allocated.
staticdeserialize(value: ProfilerSerial, out?: Profiler): Profiler
View sourceLoads serialized profiles into a profiler.
The optional out profiler is reused, but its profiles map is replaced
with a new map populated from value. Each entry creates a new
Profile. Duplicate labels are accepted by the underlying map, so
the last value for a duplicate label is retained. The method does not
validate input; use Profiler.validateSerial first
for untrusted data.
staticvalidateSerial(value: unknown): value is ProfilerSerial
View sourceChecks whether a value has the shape required by
Profiler.deserialize.
The value must be a non-null object with a profiles array. Every entry
must be a two-item array containing a string label and a value accepted by
Profile.validateSerial. Additional properties are
accepted, duplicate labels are not rejected, and numeric values are only
checked with typeof (so NaN and infinities are accepted).
