unitimer
Advanced tools
Comparing version 2.1.0 to 3.0.0
@@ -65,8 +65,8 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.unitimer = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
info: function () { | ||
return (tag ? tag + ': ' : '') + | ||
'mean=' + meanTime + | ||
'ms, total=' + totalTime + | ||
'ms, count=' + count + | ||
', min=' + minTime + | ||
'ms, max=' + maxTime + | ||
return (tag ? '[' + tag + '] ' : '') + | ||
'mean: ' + meanTime + | ||
'ms, total: ' + totalTime + | ||
'ms, count: ' + count + | ||
', min: ' + minTime + | ||
'ms, max: ' + maxTime + | ||
'ms' | ||
@@ -80,14 +80,21 @@ }, | ||
function createTimer () { | ||
var tags = Array.prototype.slice.call(arguments) | ||
function isArray (o) { | ||
return Object.prototype.toString.call(o) === '[object Array]' | ||
} | ||
// Named instances | ||
if (tags.length > 0) { | ||
var r = {} | ||
tags.forEach(function (tag) { | ||
r[tag] = createInstance(tag) | ||
}) | ||
return r | ||
function isString (o) { | ||
return (typeof o === 'string') || (o instanceof String) | ||
} | ||
function createTimer (tags) { | ||
if (isArray(tags)) { | ||
var a = [] | ||
for (var i = 0; i < tags.length; i += 1) { | ||
a.push(createInstance(tags[i])) | ||
} | ||
return a | ||
} | ||
if (isString(tags)) { | ||
return createInstance(tags) | ||
} | ||
return createInstance() | ||
@@ -94,0 +101,0 @@ } |
@@ -1,1 +0,1 @@ | ||
!function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{var g;g="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,g.unitimer=f()}}(function(){return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){function now(){return"undefined"!=typeof window.performance&&"undefined"!=typeof window.performance.now?window.performance.now():Date.now()}module.exports=now},{}],2:[function(require,module,exports){function createInstance(tag){var totalTime=0,meanTime=0,minTime=-1,maxTime=-1,count=0,startTime=null;return{start:function(){var time=now();return startTime=time,this},stop:function(){if(!startTime)return-1;var stopTime=now(),took=stopTime-startTime;return minTime=minTime===-1?took:Math.min(minTime,took),maxTime=maxTime===-1?took:Math.max(maxTime,took),totalTime+=took,count+=1,meanTime=totalTime/count,startTime=null,took},total:function(){return totalTime},mean:function(){return meanTime},count:function(){return count},min:function(){return minTime},max:function(){return maxTime},stats:function(){return{total:totalTime,mean:meanTime,count:count,min:minTime,max:maxTime}},info:function(){return(tag?tag+": ":"")+"mean="+meanTime+"ms, total="+totalTime+"ms, count="+count+", min="+minTime+"ms, max="+maxTime+"ms"},log:function(){console.log(this.info())}}}function createTimer(){var tags=Array.prototype.slice.call(arguments);if(tags.length>0){var r={};return tags.forEach(function(tag){r[tag]=createInstance(tag)}),r}return createInstance()}var now=require("./now");module.exports=createTimer},{"./now":1}]},{},[2])(2)}); | ||
!function(f){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=f();else if("function"==typeof define&&define.amd)define([],f);else{var g;g="undefined"!=typeof window?window:"undefined"!=typeof global?global:"undefined"!=typeof self?self:this,g.unitimer=f()}}(function(){return function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a="function"==typeof require&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}for(var i="function"==typeof require&&require,o=0;o<r.length;o++)s(r[o]);return s}({1:[function(require,module,exports){function now(){return"undefined"!=typeof window.performance&&"undefined"!=typeof window.performance.now?window.performance.now():Date.now()}module.exports=now},{}],2:[function(require,module,exports){function createInstance(tag){var totalTime=0,meanTime=0,minTime=-1,maxTime=-1,count=0,startTime=null;return{start:function(){var time=now();return startTime=time,this},stop:function(){if(!startTime)return-1;var stopTime=now(),took=stopTime-startTime;return minTime=minTime===-1?took:Math.min(minTime,took),maxTime=maxTime===-1?took:Math.max(maxTime,took),totalTime+=took,count+=1,meanTime=totalTime/count,startTime=null,took},total:function(){return totalTime},mean:function(){return meanTime},count:function(){return count},min:function(){return minTime},max:function(){return maxTime},stats:function(){return{total:totalTime,mean:meanTime,count:count,min:minTime,max:maxTime}},info:function(){return(tag?"["+tag+"] ":"")+"mean: "+meanTime+"ms, total: "+totalTime+"ms, count: "+count+", min: "+minTime+"ms, max: "+maxTime+"ms"},log:function(){console.log(this.info())}}}function isArray(o){return"[object Array]"===Object.prototype.toString.call(o)}function isString(o){return"string"==typeof o||o instanceof String}function createTimer(tags){if(isArray(tags)){for(var a=[],i=0;i<tags.length;i+=1)a.push(createInstance(tags[i]));return a}return isString(tags)?createInstance(tags):createInstance()}var now=require("./now");module.exports=createTimer},{"./now":1}]},{},[2])(2)}); |
@@ -54,8 +54,8 @@ var now = require('./now') | ||
info: function () { | ||
return (tag ? tag + ': ' : '') + | ||
'mean=' + meanTime + | ||
'ms, total=' + totalTime + | ||
'ms, count=' + count + | ||
', min=' + minTime + | ||
'ms, max=' + maxTime + | ||
return (tag ? '[' + tag + '] ' : '') + | ||
'mean: ' + meanTime + | ||
'ms, total: ' + totalTime + | ||
'ms, count: ' + count + | ||
', min: ' + minTime + | ||
'ms, max: ' + maxTime + | ||
'ms' | ||
@@ -69,14 +69,21 @@ }, | ||
function createTimer () { | ||
var tags = Array.prototype.slice.call(arguments) | ||
function isArray (o) { | ||
return Object.prototype.toString.call(o) === '[object Array]' | ||
} | ||
// Named instances | ||
if (tags.length > 0) { | ||
var r = {} | ||
tags.forEach(function (tag) { | ||
r[tag] = createInstance(tag) | ||
}) | ||
return r | ||
function isString (o) { | ||
return (typeof o === 'string') || (o instanceof String) | ||
} | ||
function createTimer (tags) { | ||
if (isArray(tags)) { | ||
var a = [] | ||
for (var i = 0; i < tags.length; i += 1) { | ||
a.push(createInstance(tags[i])) | ||
} | ||
return a | ||
} | ||
if (isString(tags)) { | ||
return createInstance(tags) | ||
} | ||
return createInstance() | ||
@@ -83,0 +90,0 @@ } |
{ | ||
"name": "unitimer", | ||
"version": "2.1.0", | ||
"version": "3.0.0", | ||
"description": "Universal timer (Node.js and browser)", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -193,3 +193,3 @@ const test = require('ava') | ||
timer.info(), | ||
'mean=7500ms, total=15000ms, count=2, min=5000ms, max=10000ms' | ||
'mean: 7500ms, total: 15000ms, count: 2, min: 5000ms, max: 10000ms' | ||
) | ||
@@ -200,3 +200,3 @@ t.is(typeof timer.log, 'function') | ||
test('create single with tags', (t) => { | ||
test('create single with tag', (t) => { | ||
const times = [ | ||
@@ -209,15 +209,9 @@ [5, 0], | ||
const hrtime = stubHrtime(times) | ||
const { foo, bar } = createTimer('foo', 'bar') | ||
const foo = createTimer('foo') | ||
foo.start() | ||
foo.stop() | ||
bar.start() | ||
bar.stop() | ||
t.is( | ||
foo.info(), | ||
'foo: mean=5000ms, total=5000ms, count=1, min=5000ms, max=5000ms' | ||
'[foo] mean: 5000ms, total: 5000ms, count: 1, min: 5000ms, max: 5000ms' | ||
) | ||
t.is( | ||
bar.info(), | ||
'bar: mean=10000ms, total=10000ms, count=1, min=10000ms, max=10000ms' | ||
) | ||
hrtime.restore() | ||
@@ -234,3 +228,3 @@ }) | ||
const hrtime = stubHrtime(times) | ||
const { foo, bar } = createTimer('foo', 'bar') | ||
const [ foo, bar ] = createTimer(['foo', 'bar']) | ||
foo.start() | ||
@@ -242,9 +236,9 @@ foo.stop() | ||
foo.info(), | ||
'foo: mean=5000ms, total=5000ms, count=1, min=5000ms, max=5000ms' | ||
'[foo] mean: 5000ms, total: 5000ms, count: 1, min: 5000ms, max: 5000ms' | ||
) | ||
t.is( | ||
bar.info(), | ||
'bar: mean=10000ms, total=10000ms, count=1, min=10000ms, max=10000ms' | ||
'[bar] mean: 10000ms, total: 10000ms, count: 1, min: 10000ms, max: 10000ms' | ||
) | ||
hrtime.restore() | ||
}) |
16786
428