Socket
Socket
Sign inDemoInstall

unitimer

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

unitimer - npm Package Compare versions

Comparing version 1.1.0 to 2.0.0

107

dist/unitimer.js

@@ -14,58 +14,69 @@ (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 track = {}
function unitimer () {
var totalTime = 0
var meanTime = 0
var minTime = -1
var maxTime = -1
var count = 0
var startTime = null
var timer = {
start: function (label) {
if (!track[label]) {
track[label] = {
totalTime: 0,
meanTime: 0,
minTime: -1,
maxTime: -1,
count: 0
return {
start: function () {
var time = now()
startTime = time
return this
},
stop: function () {
if (!startTime) { return -1 }
var stopTime = now()
var took = stopTime - startTime
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
return 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 'mean: ' + meanTime +
'ms, total: ' + totalTime +
'ms, count: ' + count +
', min: ' + minTime +
'ms, max: ' + maxTime +
'ms'
},
log: function () {
console.log(this.info())
}
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()
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 = timer
module.exports = unitimer
},{"./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"),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)});
!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 unitimer(){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"mean: "+meanTime+"ms, total: "+totalTime+"ms, count: "+count+", min: "+minTime+"ms, max: "+maxTime+"ms"},log:function(){console.log(this.info())}}}var now=require("./now");module.exports=unitimer},{"./now":1}]},{},[2])(2)});
var now = require('./now')
var track = {}
function unitimer () {
var totalTime = 0
var meanTime = 0
var minTime = -1
var maxTime = -1
var count = 0
var startTime = null
var timer = {
start: function (label) {
if (!track[label]) {
track[label] = {
totalTime: 0,
meanTime: 0,
minTime: -1,
maxTime: -1,
count: 0
return {
start: function () {
var time = now()
startTime = time
return this
},
stop: function () {
if (!startTime) { return -1 }
var stopTime = now()
var took = stopTime - startTime
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
return 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 'mean: ' + meanTime +
'ms, total: ' + totalTime +
'ms, count: ' + count +
', min: ' + minTime +
'ms, max: ' + maxTime +
'ms'
},
log: function () {
console.log(this.info())
}
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()
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 = timer
module.exports = unitimer
{
"name": "unitimer",
"version": "1.1.0",
"version": "2.0.0",
"description": "Universal timer (Node.js and browser)",

@@ -5,0 +5,0 @@ "main": "./lib/index.js",

@@ -22,6 +22,6 @@ # unitimer

```js
var timer = require('unitimer')
timer.start('tag')
var createTimer = require('unitimer')
var timer = createTimer().start()
setTimeout(function () {
var ms = timer.stop('tag') // ms ~= 1000.0
var ms = timer.stop() // ms ~= 1000.0
}, 1000)

@@ -32,14 +32,38 @@ ```

`start(tag)` called at the start of the section you want to measure the performance of.
### `timer.start()`
`stop(tag)` called at the end, returns interval time in milliseconds of that section.
Begin measuring interval.
`mean(tag)` returns arithmetic average in milliseconds.
### `timer.stop()`
`total(tag)` returns the total time taken for a given tag (addition of `start` / `stop` durations)
End the interval. Returns the elapsed time in milliseconds.
`count(tag)` returns the number of intervals measured.
Note: `start()` and `stop()` can be called multiple times (which affects the total time, count, mean etc.).
`min(tag)` returns the minimum interval time recorded.
### `timer.mean()`
`max(tag)` returns the maximum interval time recorded.
Returns arithmetic average in milliseconds.
### `timer.total()`
Returns the total time taken for a given (addition of `start` / `stop` durations)
### `timer.count()`
Returns the number of intervals measured.
### `timer.min()`
Returns the minimum interval time recorded.
### `timer.max()`
Returns the maximum interval time recorded.
### `timer.info()`
Returns a string summary of timer total, mean, total, min and max.
### `timer.log()`
`console.log` of `timer.info()` (see above)
const test = require('ava')
const sinon = require('sinon')
const timer = require('../lib')
const createTimer = require('../lib')

@@ -13,10 +13,29 @@ function stubHrtime (times) {

test('start label does not exist', (t) => {
const millis = timer.stop('does-not-exist')
test('start() then stop() returns the interval', (t) => {
const times = [
[4, 10],
[10, 50],
[9, 999999999],
[10, 0]
]
const hrtime = stubHrtime(times)
const a = createTimer().start()
const msA = a.stop()
t.is(msA, 6000.000040000001) // 6000.00004
const b = createTimer().start()
const msB = b.stop()
t.is(msB, 0.0000010000003385357559) // 0.000001
hrtime.restore()
})
test('stop() with no start() returns -1', (t) => {
const timer = createTimer()
const millis = timer.stop()
t.is(millis, -1)
})
test('start stop intervals', (t) => {
test('use last start() before stop()', (t) => {
const times = [
[4, 10],
[5, 10],
[10, 50],

@@ -27,7 +46,8 @@ [9, 999999999],

const hrtime = stubHrtime(times)
timer.start('A')
const msA = timer.stop('A')
t.is(msA, 6000.000040000001) // 6000.00004
timer.start('B')
const msB = timer.stop('B')
const a = createTimer().start()
a.start()
const msA = a.stop()
t.is(msA, 5000.000040000001) // 6000.00004
const b = createTimer().start()
const msB = b.stop()
t.is(msB, 0.0000010000003385357559) // 0.000001

@@ -47,8 +67,8 @@ hrtime.restore()

const hrtime = stubHrtime(times)
timer.start('A')
timer.start('B')
const msA = timer.stop('A')
timer.start('C')
const msC = timer.stop('C')
const msB = timer.stop('B')
var a = createTimer().start()
var b = createTimer().start()
const msA = a.stop()
var c = createTimer().start()
const msC = c.stop()
const msB = b.stop()
t.is(msA, 6000.000040000001) // 6000.00004

@@ -60,3 +80,3 @@ t.is(msB, 26000)

test('total time', (t) => {
test('total() is total time', (t) => {
const times = [

@@ -70,12 +90,13 @@ [5, 0],

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)
const timer = createTimer()
timer.start()
timer.stop()
timer.start()
timer.stop()
timer.stop() // Ignored
t.is(timer.total(), 15000.0)
hrtime.restore()
})
test('mean time', (t) => {
test('mean() is arithmetic average', (t) => {
const times = [

@@ -88,12 +109,12 @@ [5, 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)
const timer = createTimer().start()
timer.stop()
timer.start()
timer.stop()
t.is(timer.count(), 2)
t.is(timer.mean(), 7500.0)
hrtime.restore()
})
test('count', (t) => {
test('count() is number of intervals', (t) => {
const times = [

@@ -106,11 +127,11 @@ [5, 0],

const hrtime = stubHrtime(times)
timer.start('CT')
timer.stop('CT')
timer.start('CT')
timer.stop('CT')
t.is(timer.count('CT'), 2)
const timer = createTimer().start()
timer.stop()
timer.start()
timer.stop()
t.is(timer.count(), 2)
hrtime.restore()
})
test('max & min time', (t) => {
test('max() & min() time', (t) => {
const times = [

@@ -127,13 +148,58 @@ [5, 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)
const timer = createTimer().start()
timer.stop()
timer.start()
timer.stop()
timer.start()
timer.stop()
timer.start()
timer.stop()
t.is(timer.max(), 10000)
t.is(timer.min(), 1000)
hrtime.restore()
})
test('stats() returns a stats object', (t) => {
const times = [
[5, 0],
[10, 0],
[20, 0],
[30, 0]
]
const hrtime = stubHrtime(times)
const timer = createTimer().start()
timer.stop()
timer.start()
timer.stop()
t.deepEqual(
timer.stats(),
{
total: 15000,
mean: 7500,
count: 2,
min: 5000,
max: 10000
}
)
hrtime.restore()
})
test('info() returns a summary string', (t) => {
const times = [
[5, 0],
[10, 0],
[20, 0],
[30, 0]
]
const hrtime = stubHrtime(times)
const timer = createTimer().start()
timer.stop()
timer.start()
timer.stop()
t.deepEqual(
timer.info(),
'mean: 7500ms, total: 15000ms, count: 2, min: 5000ms, max: 10000ms'
)
t.is(typeof timer.log, 'function')
hrtime.restore()
})
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc