https://github.com/bkrem/node-perf-timer
🏃 ⏱ Measuring elapsed time in Node, made simple.
https://github.com/bkrem/node-perf-timer
developer-experience developer-tools diffing node nodejs performance timer
Last synced: 15 days ago
JSON representation
🏃 ⏱ Measuring elapsed time in Node, made simple.
- Host: GitHub
- URL: https://github.com/bkrem/node-perf-timer
- Owner: bkrem
- License: mit
- Created: 2017-09-16T12:48:38.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2017-10-05T14:10:52.000Z (over 7 years ago)
- Last Synced: 2025-03-24T23:02:07.604Z (29 days ago)
- Topics: developer-experience, developer-tools, diffing, node, nodejs, performance, timer
- Language: JavaScript
- Homepage:
- Size: 69.3 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# node-perf-timer [](https://travis-ci.org/bkrem/node-perf-timer) [](https://coveralls.io/github/bkrem/node-perf-timer)
Measuring elapsed time in Node, made simple.
## Motivation
Node's native timing function – [`process.hrtime()`](https://nodejs.org/api/process.html#process_process_hrtime_time) – is extremely precise, but low-level and messy to use for profiling without any abstraction on top of it.`node-perf-timer` aims to provide a simple API to provide a more intuitive developer experience when measuring elapsed time in Node applications.
## Requirements
* Node v4.x or higher## Installation
```
npm i --save-dev node-perf-timer
```
or
```
yarn add node-perf-timer --dev
```## Usage
```js
const perfTimer = require('node-perf-timer')// start a timer
perfTimer.start();functionToBeMeasured();
// returns diff in nanoseconds & logs (by default):
// Message: functionToBeMeasured()
// Duration: 0 s, 3.589862 ms
const nsDiff = perfTimer.stopAndDiff('functionToBeMeasured()');
```## Configuration
By default, the configuration object takes the following shape:
```js
const opts = {
shouldPrint: true, // should diffs be logged to the console?
precision: undefined, // e.g. `4`; how many decimal places to use for millisecond formatting
defaultMessage: undefined, // e.g. `"hello bottleneck!"`; default message for each diff being logged
};
```## API
* `config(configObj)` - Adjust default opts for all future `perfTimer` calls.
* `start()` - Starts a new timer and returns the absolute start time in nanoseconds.
* `stop()` - Stops the current timer and returns the absolute end time in nanoseconds.
* `diff(nsStartTime, nsEndTime)` - Accepts a start & end time in nanoseconds and returns the difference.
* `stopAndDiff(message?)` - Combines stop() & diff(). If `opts.shouldPrint` is set, the diff is logged to the console with `message`, `opts.defaultMessage` or simply the duration.
* `stopAndRestart()` - Stops timer, starts a new one and returns the nanosecond diff of the stopped timer.[Full API Documentation](/docs/index.md)
## Examples
Examples for a number of common use cases can be found [here (WIP)](/examples).## License
MIT