node package manager


timing functions using process.hrtime


General purpose timing functions that use process.hrtime() Now with process.hrtime shim for working in browsers with browserify (which shims process itself).

Method Overview

  • time.sinceBegin - call to get time since time was constructed
  • time.sinceLast - call to get time since last call
  • time.every - call a function every interval in nanoseconds
  • time.loop // same as above
  • time.avg - call to get average interval between all calls to same
npm install since-when

These timing functions give you more precise readings by using Node's Process.hrtime(). hrtime() returns an array like this:

[seconds, partial nanoseconds]

where nanoseconds is in addition to seconds, like so:

    var time = process.hrtime()
    var totalNanoSeconds = (time[0] * 1e9) + time[1]

Although it returns nanoseconds, it can only really be considered accurate to the millisecond when used in the browser.

If you pass the return value of a previous hrtime() to process.hrtime(lastTime), you are returned a delta array.

Example, from examples/fib.js

var time = require('../');
var fib = function(n){
  var t = new time()
  var nacho = 0
    , p1 = 0
    , p2 = 0
    , a = 0;
  while(<= n){
    nacho = p1 + p2;
    p2 = p1;
    p1 = (=== 1) ? nacho += 1 : nacho
  return [nacho, t.sinceBegin()]
var f = fib(100)
var ns = f[1][0] * 1e9 + f[1][1]
console.log('calculated answer to be: ' + f[0] + ' in %sns', ns)



Returns an hrtime array [seconds, nanoseconds] of time since new Time() was called

var Time = require('since-when');
var time = new Time();
setInterval(tick, 500);
function tick(){

Time.sinceBeginNS() same as above, but returns total time in nanoseconds

Time.sinceLast() Returns hrtime array of time since last sinceLast().

var Time = require('since-when');
var time = new Time();
setInterval(tick, 0);
function tick(){

Time.sinceLastNS() same as above, but returns total time in nanoseconds

Time.loop(nanoseconds, fn, boolean) || Time.every(...)* This calls your fn every nanoseconds. The boolean argument decides whether to call yr function immediately, or wait the nanoseconds first. Defaults to the waiting the interval. Your function is called with two arguments, loop[function] and interval[number]. Call loop() to keep the loop going! Don't call it and it stops! Interval is the actual time since the last cycle, and should be close to the value nanoseconds you passed.

This method is somewhat optimized. It does a little math, naively, you might say, to keep polling to a minimum, and not fill Node's event cue with a million process.nextTick()s.

var Time = require('since-when');
var time = new Time();
// every billion nanoseconds tock() 
time.every(1e9, tock);
function tock(tick, interval){
  console.log(interval + ' time in nanoseconds passed')


Call this and it returns the average interval between all such calls.

var Time = require('../');
var time = new Time();
var t2 = new Time();
// this function outputs the average interval of calls 
// which should normalize to 10 ms over time 
// it also takes into account how long it takes    
// to compute the average 
var averages = function(){
  var i = Math.random() * 20
  var t = t2.sinceLast()
    , avg = time.avg()
    , dur = t2.sinceLast()[1] / 1e6
  console.log(avg / 1e6)
  var t = setTimeout(averages, i - Math.ceil(dur))