Skip to content

Instantly share code, notes, and snippets.

@sheldonh
Created March 24, 2015 13:05
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 sheldonh/bc5bb9ab2f2744ef1a3f to your computer and use it in GitHub Desktop.
Save sheldonh/bc5bb9ab2f2744ef1a3f to your computer and use it in GitHub Desktop.
log_duration.coffee
# Logs timing of an operation:
#
# * Start a clock.
# * Calls the `operation`, passing in a `done` function that will:
# * Stop the clock.
# * Log the title of the operation with its elapsed duration and optional outcome.
# If the operation calls the `done` function with no outcome, "completed" is used.
#
# Example:
#
# request = require("request")
#
# do_request = (url, title = "Request", callback) ->
# log_duration title, (stop_clock) ->
# request.get url, (error, response, body) ->
# stop_clock(response?.statusCode or "500")
# callback(error, body)
#
log_duration = (title, operation) ->
start = Date.now()
done = (outcome) ->
outcome ||= "completed"
end = Date.now()
duration = (end - start) / 1000
console.log "#{title} #{outcome} in #{duration} seconds"
operation(done)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment