https://github.com/catdad/hitime
:alarm_clock: hi-res timer for Node, wrapped in some pretty helpers
https://github.com/catdad/hitime
npm timer
Last synced: about 2 months ago
JSON representation
:alarm_clock: hi-res timer for Node, wrapped in some pretty helpers
- Host: GitHub
- URL: https://github.com/catdad/hitime
- Owner: catdad
- Created: 2016-06-19T15:03:30.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-06-23T23:04:52.000Z (almost 8 years ago)
- Last Synced: 2025-03-19T14:02:45.678Z (3 months ago)
- Topics: npm, timer
- Language: JavaScript
- Homepage:
- Size: 20.5 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# hitime
[![Build][1]][2]
[![Test Coverage][3]][4]
[![Code Climate][5]][6]
[![Downloads][7]][8]
[![Version][9]][8]
[![Dependency Status][10]][11][1]: https://travis-ci.org/catdad/hitime.svg?branch=master
[2]: https://travis-ci.org/catdad/hitime[3]: https://codeclimate.com/github/catdad/hitime/badges/coverage.svg
[4]: https://codeclimate.com/github/catdad/hitime/coverage[5]: https://codeclimate.com/github/catdad/hitime/badges/gpa.svg
[6]: https://codeclimate.com/github/catdad/hitime[7]: https://img.shields.io/npm/dm/hitime.svg
[8]: https://www.npmjs.com/package/hitime
[9]: https://img.shields.io/npm/v/hitime.svg[10]: https://david-dm.org/catdad/hitime.svg
[11]: https://david-dm.org/catdad/hitime:alarm_clock: Hi-res timer for Node, wrapped in some pretty helpers.
## Install
```bash
npm install -S hitime
```## Use
To get a simple high resolution timestamp:
```javascript
var hitime = require('hitime');var timestamp = hitime();
```This will return a decimal number, in milliseconds (so you can still do mental math), accurate to the nanosecond, using Node's native `process.hrtime`. This is a relative time, measured from the time that the module was loaded.
You can compare two timestamps with regular math, because they are just numbers:
```javascript
var a = hitime();
var b = hitime();var duration = b - a;
```### Using named timers
Similarly to Chrome's `console.time`, this module gives you named timers, so you can keep easier track of various tasks.
```javascript
var timer = hitime.Timer();timer.start('async work');
doAsyncWork(function(err) {
// with high-resolution time, you want
// to make sure you call this as soon as possible
timer.end('async work');// check for an error now... if statements do cost
// time, you knowtimer.start('heavy work');
...
timer.end('heavy work');
// at some point in the future
var report = timer.report();console.log('async work in %s ms', report['async work'].duration);
console.log('heavy work in %s ms', report['heavy work'].duration);
});
```Any timers that have been completed will appear in the report, containing the following values:
- `start`: the relative start time of the timer
- `end`: the relative end time of the timer
- `duration`: the total time for the timer