stopwatch-lite
A stopwatch for all your time-measuring needs
Installing
npm install stopwatch-lite
Features
start()
starts stopwatch, has no effect if the stopwatch is running
stop()
stops stopwatch, has no effect if the stopwatch is stopped
reset()
stops stopwatch and sets time to 0
read()
returns number of milliseconds on stopwatch
To use multiple stopwatches, pass in the name of a particular stopwatch. Stopwatch names can be any value (including functions, objects, or any primitive).
Usage
import stopwatch from "stopwatch-lite";
stopwatch.start();
...
stopwatch.stop();
console.log(stopwatch.read());
import sw from "stopwatch-lite";
sw.start("example");
sw.start(123);
foo().then(() => {
sw.stop("example");
console.log(`foo execution time: ${sw.read("example")} ms`);
});
bar().then(() => {
sw.stop(123);
console.log(`bar execution time: ${sw.read(123)} ms`);
});
import sw from "stopwatch-lite";
let totalExecutionTime = 0;
for (let i = 0; i < 42; i++) {
sw.start();
foo();
totalExecutionTime += sw.read();
sw.reset();
}
console.log(`average execution time: ${totalExecutionTime / 42} ms`);