import * as I from 'immutable';


const now = (() => {
  if (typeof performance != 'undefined' && performance.now)
    return performance.now.bind(performance);
  else if (typeof process != 'undefined' && process.hrtime)
    return () => {
      const [sec, nsec] = process.hrtime();
      return sec * 1000 + nsec / 1000000;
    };
  else
    return Date.now.bind(Date);
})();


export function timer() {
  let _last = now();

  return () => {
    const previous = _last;
    _last = now();
    return _last - previous;
  };
};


export function timers() {
  const _accumulated = {};
  const _start = {};

  return {
    start(key) {
      if (key != null && _start[key] == null) {
        _start[key] = now();
      }
    },
    stop(key) {
      if (_start[key] != null) {
        _accumulated[key] = (_accumulated[key] || 0.0) + now() - _start[key];
        _start[key] = null;
      }
    },
    stopAll() {
      for (const k in _start)
        this.stop(k);
    },
    switchTo(key) {
      this.stopAll();
      this.start(key);
    },
    current() {
      for (const k in _start) {
        if (_start[k] != null) {
          this.stop(k);
          this.start(k);
        }
      }

      const result = {};
      for (const k in _accumulated)
        result[k] = _accumulated[k];
      return result;
    }
  };
};

@@ -598,16 +598,37 @@
 rt = %7B%7D;
+%0A  const _count = %7B%7D;
 %0A%0A  retu
@@ -722,24 +722,69 @@
 y%5D = now();%0A
+        _count%5Bkey%5D = (_count%5Bkey%5D %7C%7C 0) + 1%0A
       %7D%0A    
@@ -1333,16 +1333,17 @@
 lt%5Bk%5D = 
+%5B
 _accumul
@@ -1349,16 +1349,28 @@
 lated%5Bk%5D
+, _count%5Bk%5D%5D
 ;%0A      

include execution counts for timers()