Skip to content

Instantly share code, notes, and snippets.

@olange
Created May 19, 2016 16:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save olange/379bde69c188597a559579dbd9c94f24 to your computer and use it in GitHub Desktop.
Save olange/379bde69c188597a559579dbd9c94f24 to your computer and use it in GitHub Desktop.
Measuring a duration of execution in Node.js
##
# Utility function to measure time intervals. Polymorphic return value:
#
# * returns either the current high-resolution real time, given no argument;
# * or the time elapsed since a given high-resolution real time, that was
# retrieved from a previous call to this function.
#
# Usage:
# t0 = hrtime()
# duration = hrtime( t0) # expressed in milliseconds
#
# See also:
# [Node.js API › process.hrtime()](https://nodejs.org/api/process.html#process_process_hrtime)
#
hrtime = (since) ->
return process.hrtime() unless since?
end = process.hrtime( since)
Math.round(( end[ 0] * 1e3) + ( end[ 1] / 1e6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment