Skip to content
Get the current time in microseconds
C++ JavaScript Python
Branch: master
Clone or download

Latest commit

Latest commit c1e3617 Feb 4, 2019

Files

Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src simplify by disabling exceptions Jan 28, 2019
.appveyor.yml try to support Node v4 and v6 on macOS Jan 31, 2019
.gitignore update .gitignore Jan 28, 2019
.npmignore add .npmignore for build/ May 15, 2014
.travis.yml try to support Node v4 and v6 on macOS Jan 31, 2019
LICENSE
README.md more README cleanup Feb 4, 2019
binding.gyp try to support Node v4 and v6 on macOS Jan 31, 2019
index.js convert to prebuildify Jan 28, 2019
package.json 3.0.0 Feb 4, 2019
test.js reformat to standardjs style May 12, 2016

README.md

node-microtime

Travis Build AppVeyor Build npm npm

Date.now() will only give you accuracy in milliseconds. This module calls gettimeofday(2) to get the time in microseconds and provides it in a few different formats. The same warning from that function applies: The resolution of the system clock is hardware dependent, and the time may be updated continuously or in ``ticks.''

N-API Support

As of version 3.0.0, this library is built using the N-API library. This should allow you to upgrade to newer versions of Node.js without having to reinstall / rebuild this library.

Version 3.0.0 and later also bundle prebuilt binaries for common systems in the npm package itself. These will be used if the version of Node you are using supports N-API (>= v6.14.2), otherwise the binary will be recompiled on install.

Installation

npm install microtime

Usage

microtime.now()

Get the current time in microseconds as an integer.

microtime.nowDouble()

Get the current time in seconds as a floating point number with microsecond accuracy (similar to time.time() in Python and Time.now.to_f in Ruby).

microtime.nowStruct()

Get the current time and return as a list with seconds and microseconds (matching the return value of gettimeofday(2)).

Example

> var microtime = require('microtime')
> microtime.now()
1297448895297028
> microtime.nowDouble()
1297448897.600976
> microtime.nowStruct()
[ 1297448902, 753875 ]

Estimating clock resolution

Starting with version 0.1.3, there is a test script that tries to guess the clock resolution. You can run it with npm test microtime. Example output:

microtime.now() = 1298960083489806
microtime.nowDouble() = 1298960083.511521
microtime.nowStruct() = [ 1298960083, 511587 ]

Guessing clock resolution...
Clock resolution observed: 1us
You can’t perform that action at this time.