Comparing version 1.0.1 to 1.1.0
@@ -14,19 +14,58 @@ (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){ | ||
var starts = {} | ||
var track = {} | ||
var unitimer = { | ||
var timer = { | ||
start: function (label) { | ||
starts[label] = now() | ||
if (!track[label]) { | ||
track[label] = { | ||
totalTime: 0, | ||
meanTime: 0, | ||
minTime: -1, | ||
maxTime: -1, | ||
count: 0 | ||
} | ||
} | ||
var time = now() | ||
track[label].startTime = time | ||
}, | ||
stop: function (label) { | ||
if (starts[label]) { | ||
return now() - starts[label] | ||
} | ||
return -1 | ||
if (!track[label]) { return -1 } | ||
var o = track[label] | ||
if (!o.startTime) { return -1 } | ||
var stopTime = now() | ||
var took = stopTime - o.startTime | ||
o.startTime = null | ||
o.lastTook = took | ||
o.minTime = o.minTime === -1 ? took : Math.min(o.minTime, took) | ||
o.maxTime = o.maxTime === -1 ? took : Math.max(o.maxTime, took) | ||
o.totalTime += took | ||
o.count += 1 | ||
o.meanTime = o.totalTime / o.count | ||
return took | ||
}, | ||
total: function (label) { | ||
if (!track[label]) { return -1 } | ||
return track[label].totalTime | ||
}, | ||
mean: function (label) { | ||
if (!track[label]) { return -1 } | ||
return track[label].meanTime | ||
}, | ||
count: function (label) { | ||
if (!track[label]) { return -1 } | ||
return track[label].count | ||
}, | ||
max: function (label) { | ||
if (!track[label]) { return -1 } | ||
return track[label].maxTime | ||
}, | ||
min: function (label) { | ||
if (!track[label]) { return -1 } | ||
return track[label].minTime | ||
} | ||
} | ||
module.exports = unitimer | ||
module.exports = timer | ||
},{"./now":1}]},{},[2])(2) | ||
}); |
@@ -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){var now=require("./now"),starts={},unitimer={start:function(label){starts[label]=now()},stop:function(label){return starts[label]?now()-starts[label]:-1}};module.exports=unitimer},{"./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){var now=require("./now"),track={},timer={start:function(label){track[label]||(track[label]={totalTime:0,meanTime:0,minTime:-1,maxTime:-1,count:0});var time=now();track[label].startTime=time},stop:function(label){if(!track[label])return-1;var o=track[label];if(!o.startTime)return-1;var stopTime=now(),took=stopTime-o.startTime;return o.startTime=null,o.lastTook=took,o.minTime=o.minTime===-1?took:Math.min(o.minTime,took),o.maxTime=o.maxTime===-1?took:Math.max(o.maxTime,took),o.totalTime+=took,o.count+=1,o.meanTime=o.totalTime/o.count,took},total:function(label){return track[label]?track[label].totalTime:-1},mean:function(label){return track[label]?track[label].meanTime:-1},count:function(label){return track[label]?track[label].count:-1},max:function(label){return track[label]?track[label].maxTime:-1},min:function(label){return track[label]?track[label].minTime:-1}};module.exports=timer},{"./now":1}]},{},[2])(2)}); |
var now = require('./now') | ||
var starts = {} | ||
var track = {} | ||
var timer = { | ||
start: function (label) { | ||
starts[label] = now() | ||
if (!track[label]) { | ||
track[label] = { | ||
totalTime: 0, | ||
meanTime: 0, | ||
minTime: -1, | ||
maxTime: -1, | ||
count: 0 | ||
} | ||
} | ||
var time = now() | ||
track[label].startTime = time | ||
}, | ||
stop: function (label) { | ||
if (starts[label]) { | ||
return now() - starts[label] | ||
} | ||
return -1 | ||
if (!track[label]) { return -1 } | ||
var o = track[label] | ||
if (!o.startTime) { return -1 } | ||
var stopTime = now() | ||
var took = stopTime - o.startTime | ||
o.startTime = null | ||
o.lastTook = took | ||
o.minTime = o.minTime === -1 ? took : Math.min(o.minTime, took) | ||
o.maxTime = o.maxTime === -1 ? took : Math.max(o.maxTime, took) | ||
o.totalTime += took | ||
o.count += 1 | ||
o.meanTime = o.totalTime / o.count | ||
return took | ||
}, | ||
total: function (label) { | ||
if (!track[label]) { return -1 } | ||
return track[label].totalTime | ||
}, | ||
mean: function (label) { | ||
if (!track[label]) { return -1 } | ||
return track[label].meanTime | ||
}, | ||
count: function (label) { | ||
if (!track[label]) { return -1 } | ||
return track[label].count | ||
}, | ||
max: function (label) { | ||
if (!track[label]) { return -1 } | ||
return track[label].maxTime | ||
}, | ||
min: function (label) { | ||
if (!track[label]) { return -1 } | ||
return track[label].minTime | ||
} | ||
@@ -15,0 +54,0 @@ } |
{ | ||
"name": "unitimer", | ||
"version": "1.0.1", | ||
"version": "1.1.0", | ||
"description": "Universal timer (Node.js and browser)", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -23,6 +23,22 @@ # unitimer | ||
var timer = require('unitimer') | ||
timer.start('label') | ||
timer.start('tag') | ||
setTimeout(function () { | ||
var ms = timer.stop('label') // ms ~= 1000.0 | ||
var ms = timer.stop('tag') // ms ~= 1000.0 | ||
}, 1000) | ||
``` | ||
## API | ||
`start(tag)` called at the start of the section you want to measure the performance of. | ||
`stop(tag)` called at the end, returns interval time in milliseconds of that section. | ||
`mean(tag)` returns arithmetic average in milliseconds. | ||
`total(tag)` returns the total time taken for a given tag (addition of `start` / `stop` durations) | ||
`count(tag)` returns the number of intervals measured. | ||
`min(tag)` returns the minimum interval time recorded. | ||
`max(tag)` returns the maximum interval time recorded. |
@@ -5,2 +5,10 @@ const test = require('ava') | ||
function stubHrtime (times) { | ||
let index = 0 | ||
function fn () { | ||
return times[index++] | ||
} | ||
return sinon.stub(process, 'hrtime', fn) | ||
} | ||
test('start label does not exist', (t) => { | ||
@@ -18,7 +26,3 @@ const millis = timer.stop('does-not-exist') | ||
] | ||
let index = 0 | ||
function fn () { | ||
return times[index++] | ||
} | ||
var hrtime = sinon.stub(process, 'hrtime', fn) | ||
const hrtime = stubHrtime(times) | ||
timer.start('A') | ||
@@ -42,8 +46,3 @@ const msA = timer.stop('A') | ||
] | ||
let index = 0 | ||
function fn () { | ||
return times[index++] | ||
} | ||
var hrtime = sinon.stub(process, 'hrtime', fn) | ||
const hrtime = stubHrtime(times) | ||
timer.start('A') | ||
@@ -58,4 +57,79 @@ timer.start('B') | ||
t.is(msC, 1000.0000999999975) // 1000.000100 | ||
hrtime.restore() | ||
}) | ||
test('total time', (t) => { | ||
const times = [ | ||
[5, 0], | ||
[10, 0], | ||
[20, 0], | ||
[30, 0], | ||
[50, 0] // Not used | ||
] | ||
const hrtime = stubHrtime(times) | ||
timer.start('TT') | ||
timer.stop('TT') | ||
timer.start('TT') | ||
timer.stop('TT') | ||
timer.stop('TT') // Ignored | ||
t.is(timer.total('TT'), 15000.0) | ||
hrtime.restore() | ||
}) | ||
test('mean time', (t) => { | ||
const times = [ | ||
[5, 0], | ||
[10, 0], | ||
[20, 0], | ||
[30, 0] | ||
] | ||
const hrtime = stubHrtime(times) | ||
timer.start('MT') | ||
timer.stop('MT') | ||
timer.start('MT') | ||
timer.stop('MT') | ||
t.is(timer.count('MT'), 2) | ||
t.is(timer.mean('MT'), 7500.0) | ||
hrtime.restore() | ||
}) | ||
test('count', (t) => { | ||
const times = [ | ||
[5, 0], | ||
[10, 0], | ||
[20, 0], | ||
[30, 0] | ||
] | ||
const hrtime = stubHrtime(times) | ||
timer.start('CT') | ||
timer.stop('CT') | ||
timer.start('CT') | ||
timer.stop('CT') | ||
t.is(timer.count('CT'), 2) | ||
hrtime.restore() | ||
}) | ||
test('max & min time', (t) => { | ||
const times = [ | ||
[5, 0], | ||
[10, 0], | ||
[20, 0], | ||
[30, 0], | ||
[40, 0], | ||
[41, 0], | ||
[100, 0], | ||
[102, 0] | ||
] | ||
const hrtime = stubHrtime(times) | ||
timer.start('MM') | ||
timer.stop('MM') | ||
timer.start('MM') | ||
timer.stop('MM') | ||
timer.start('MM') | ||
timer.stop('MM') | ||
timer.start('MM') | ||
timer.stop('MM') | ||
t.is(timer.max('MM'), 10000) | ||
t.is(timer.min('MM'), 1000) | ||
hrtime.restore() | ||
}) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12857
262
44
1