Turn audio into a shareable video. forked from nypublicradio/audiogram

profiler.js 966B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. function Profiler() {
  2. this._times = {};
  3. return this;
  4. };
  5. Profiler.prototype.start = function(key) {
  6. this.end(this._current);
  7. this._current = key;
  8. this._times[this._current] = { start: Date.now() };
  9. return this;
  10. };
  11. Profiler.prototype.size = function(size) {
  12. if (!arguments.length) return this._size;
  13. this._size = size;
  14. return this;
  15. };
  16. Profiler.prototype.end = function(key) {
  17. if (key in this._times) this._times[key].end = Date.now();
  18. return this;
  19. };
  20. Profiler.prototype.print = function(size) {
  21. var rows = [],
  22. row;
  23. this.end(this._current);
  24. for (var key in this._times) {
  25. row = { key: key, time: this._times[key].end - this._times[key].start };
  26. if (this._size) row.per = row.time / this._size;
  27. rows.push(row);
  28. }
  29. return rows.map(function(row){
  30. return row.key + ": " + Math.round(row.time) + "ms total" + (row.per ? ", " + Math.round(row.per) + "ms per" : "");
  31. }).join("\n");
  32. };
  33. module.exports = Profiler;