Profiler

View source

Collects 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) // milliseconds

Constructor


constructor()

Properties


Methods


set(label: string): Profile

View source

Creates 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.

Parameters

NameDescriptionDefault
label

Name used to retrieve the profile.

—

Returns

The newly stored profile.

get(label: string): Profile

View source

Gets the profile stored for a label.

Parameters

NameDescriptionDefault
label

Name of the profile to retrieve.

—

Returns

The profile, or undefined when no entry exists.

start(label: string): void

View source

Starts 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.

Parameters

NameDescriptionDefault
label

Name of the interval to start.

—

end(label: string): void

View source

Ends 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.

Parameters

NameDescriptionDefault
label

Name of the interval to end.

—

staticserialize(value: Profiler): ProfilerSerial

View source

Converts 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.

Parameters

NameDescriptionDefault
value

Profiler to serialize.

—

Returns

Serialized profile entries.

staticdeserialize(value: ProfilerSerial, out?: Profiler): Profiler

View source

Loads 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.

Parameters

NameDescriptionDefault
value

Serialized profile entries.

—
out?

Profiler to overwrite.

...

Returns

The populated profiler.

staticvalidateSerial(value: unknown): value is ProfilerSerial

View source

Checks 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).

Parameters

NameDescriptionDefault
value

Value to check.

—

Returns

true when every serialized entry has the required shape.