perf.js 548 B

1234567891011121314151617181920212223242526
  1. 'use strict'
  2. var log = require('npmlog')
  3. var EventEmitter = require('events').EventEmitter
  4. var perf = new EventEmitter()
  5. module.exports = perf
  6. var timings = {}
  7. process.on('time', time)
  8. process.on('timeEnd', timeEnd)
  9. perf.on('time', time)
  10. perf.on('timeEnd', timeEnd)
  11. function time (name) {
  12. timings[name] = Date.now()
  13. }
  14. function timeEnd (name) {
  15. if (name in timings) {
  16. perf.emit('timing', name, Date.now() - timings[name])
  17. delete timings[name]
  18. } else {
  19. log.silly('timing', "Tried to end timer that doesn't exist:", name)
  20. }
  21. }