timer.js 614 B

12345678910111213141516171819202122232425262728293031323334353637
  1. 'use strict';
  2. const { nano } = require('./utils');
  3. class Timer {
  4. constructor() {
  5. this.date = {};
  6. this.hr = {};
  7. }
  8. start() {
  9. this.date.start = new Date();
  10. this.hr.start = process.hrtime();
  11. return this;
  12. }
  13. end() {
  14. this.date.end = new Date();
  15. this.hr.end = process.hrtime();
  16. this.hr.duration = process.hrtime(this.hr.start);
  17. return this;
  18. }
  19. get diff() {
  20. return nano(this.hr.end) - nano(this.hr.start);
  21. }
  22. get duration() {
  23. return this.hr.duration ? require('pretty-time')(this.hr.duration) : '';
  24. }
  25. }
  26. /**
  27. * Expose `Timer`
  28. */
  29. module.exports = Timer;