Package Exports
- timer-node
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (timer-node) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
timer-node
A simple timer with pause/resume capability that enables recording elapsed time and format the result.
Table of Contents
Install
npm install --save timer-nodeAPI
require
const { Timer } = require('timer-node');import
import { Timer } from 'timer-node';Construction
const timer = new Timer('test-timer');.start()
starts the timer. can be chained.
timer.start();.isStarted()
returns true if the timer is started, false if timer is not started or it's been stopped.
console.log(timer.isStarted()); // true.pause()
pause the timer and memoize the elapsed time. can be chained.
timer.pause();.isPaused()
returns true if the timer is paused, false if timer is not started or it's been resumed after a pause.
console.log(timer.isPaused()); // true.resume()
resume the timer by creating a new starting time. can be chained.
timer.resume();.time()
return the elapsed time as an object. It can be called while the timer is running or when it is paused or stopped and will return the current recorded time plus the memoized pause times.
s: secondsms: millisecondsus: microsecondsns: nanoseconds
console.log(timer.time()); // { s: 14, ms: 496, us: 303, ns: 508 }
console.log(timer.time()); // { s: 21, ms: 321, us: 487, ns: 783 }
console.log(timer.time()); // { s: 36, ms: 674, us: 616, ns: 145 }.stop()
stops the timer. can be chained. The timer can be started again by calling .start() which clears recorded values.
timer.stop();
console.log(timer.time()); // { s: 85, ms: 39, us: 492, ns: 853 }
console.log(timer.time()); // { s: 85, ms: 39, us: 492, ns: 853 }.isStopped()
returns true if the timer is stopped, false otherwise.
console.log(timer.isStopped()); // true.format([template])
formats the elapsed time using a custom or default template. The function replaces the time fractions placeholders in a string. Placeholders are:
%lblfor timer label.%sfor seconds.%msfor milliseconds.%usfor microseconds.%nsfor nanoseconds.
// using the default template
console.log(timer.format()); // test-timer: 4 s, 254 ms, 782 us, 615 ns
// using a custom template
const custom = '%lbl [%s] s [%ms] ms';
console.log(timer.format(custom)); // test-timer [4] s [254] ms.clear()
clears the timer values. can be started again by calling .start().
timer.clear();
console.log(timer.time()); // nullTimer.benchmark(fn)
creates a benchmark timer for a function call.
const fn = (a) => {
let sum = 0;
for (let i = 0; i < 10000000; i += 1) {
sum += a * i;
}
return sum;
}
const benchmark = Timer.benchmark(fn.bind(fn, 5));
console.log(benchmark.milliseconds()); // 29
console.log(benchmark.format()); // bound fn: 0 s, 29 ms, 43 us, 882 nsBuild
grunt buildLicense
The MIT License. Full License is here