'use strict';

function Timer () {
    this.times = {};
}

module.exports = Timer;

Timer.prototype.start = function (label) {
    this.timeIt(label, 'start');
};

Timer.prototype.end = function (label) {
    this.timeIt(label, 'end');
};

Timer.prototype.timeIt = function (label, what) {
    this.times[label] = this.times[label] || {};
    this.times[label][what] = Date.now();
};

Timer.prototype.getTimes = function () {
    var self = this;
    var times = {};

    Object.keys(this.times).forEach(function (label) {
        var stat = self.times[label];
        if (stat.start && stat.end) {
            var elapsed = stat.end - stat.start;
            if (elapsed > 0) {
                times[label] = elapsed;
            }
        }
    });

    return times;
};

'use strict';

module.exports = class Timer {
    constructor () {
        this.times = {};
    }

    start (label) {
        this.timeIt(label, 'start');
    }

    end (label) {
        this.timeIt(label, 'end');
    }

    timeIt (label, what) {
        this.times[label] = this.times[label] || {};
        this.times[label][what] = Date.now();
    }

    getTimes () {
        const times = {};

        for (const [label, stat] of Object.entries(this.times)) {
            if (stat.start && stat.end) {
                const elapsed = stat.end - stat.start;
                if (elapsed > 0) {
                    times[label] = elapsed;
                }
            }
        }

        return times;
    }
};

Apply ES6