// 1. Import the library
const jsProfiler = require('js-profiler');
// 2. Configure js-profiler
const options = {
json: true,
precision: 2,
unit: 'µs'
};
// 3. Run the profiler
jsProfiler.run(options)
.then((report) => {
console.log(JSON.stringify(report, null, 2));
});
The js-profiler
module exports an object providing two functions:
Returns an array of available profiles. Each profile is represented by a name and description.
default: 1
The verbosity to use for profile description output.
Use the constants provided in verbosity.
Calling run()
starts profile execution and returns a Promise
which resolves with the profiling results.
The library provides analogous options to the CLI.
default: false
If set to true, a ConsoleReporter is attached, writing profile results to the console.
default: 1000
Specify the number of iterations per profiled function.
default: false
If set to true, a JSONReporter is attached, writing profile results to the console in JSON format.
default: 1000
Specify the magnitude of test data, i.e. the number of items to supply to each profiled function.
default: false
If set to true, memory consumption is measured and reported.
ATTENTION: Enabling memory reporting significantly slows down profile execution.
default: { time: 4, memory: 4 }
Specify the number of decimal places to output for results.
default: undefined
If specified, only profiles with the names provided are executed.
jsperformance.run({
profiles: [
'recursion',
'loops'
]
}).then((report) => {
console.log(report);
});
default: { time: 'auto', memory: 'auto' }
Specify the unit to use for time output. Possible values are:
auto
: Automatically convert between units where appropriate.ms
ormilliseconds
: Millisecondsµs
ormicroseconds
: MicrosecondsB
: ByteKB
: KilobyteMB
: Megabyte
Use the constants found in units.
default: 1
Set output verbosity. Possible values are:
0
Quiet. Output only results1
Normal. Output name, description and result of profiled functions.2
Verbose. Output verbose data.
Use the constants found in verbosity;