timer.js 786 Bytes
'use strict';

/**
 * 计时类
 * @example 
 * let timer = new Timer();
 * timer.put('profile');
 * timer.put('proflie'); // console output: 12.14 
 * 
 * @author: hbomb<qiqi.zhou@yoho.cn>
 * @date: 2016/05/07
 */

class Timer {
    constructor() {
        this.timers = {};
    }
    
    /**
     * 打点计时
     */
    put(label) {
        let labelTime = this.timers[label];
        
        if (labelTime) {
            let duration = process.hrtime(labelTime);
            return this._round(duration[1]);
        } else {
            this.timers[label] = process.hrtime();
        }
    }
    
    /**
     * 格式化成毫秒
     * @param {Number} value 纳秒
     */
    _round(value) {
        return Math.round(value / 10000) / 100;
    }

}

 module.exports = Timer;