Skip to content

Allowing measurement of multiple time slots #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,13 @@ myTimer.getDuration() // 18000

#### `.measureStart(label)`

Start a high-performance measurement with an associated label, you need to use
Start or continue a high-performance measurement with the associated label, you need to use
the same label to stop measurement, so make sure you've saved it

#### `.measurePause(label)`

Pause the measurement with the associated label

#### `.measureStop(label)`

Stop the measument with the associated label, returns the number of elapsed ms
Expand All @@ -171,12 +175,23 @@ Example:

```javascript

myTimer.measureStart('label1');
var label = 'label1';

myTimer.measureStart(label);
var a = [];
for (var i = 10000000; i >= 0; i--) {
a.push(i * Math.random());
};
myTimer.measureStop('label1'); // 276 i.e.
myTimer.measurePause(label);

// do something else

myTimer.measureStart(label);
for (var i = 0, sum = 0; i < a.length; i++) {
sum += a[i];
};
var time_elapsed = myTimer.measureStop(label);

```

> Note!
Expand Down
34 changes: 32 additions & 2 deletions src/timer.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,42 @@
}

Timer.prototype.measureStart = function (label) {
this._.measures[label || ''] = +new Date
var measure

if (!this._.measures[label || '']) {
measure = this._.measures[label || ''] = {
value: 0,
}
}
else measure = this._.measures[label || '']

measure.timeStamp = +new Date()
measure.paused = false

return this
}

Timer.prototype.measurePause = function (label) {
var measure = this._.measures[label || '']
if (measure && !measure.paused) {
measure.value += +new Date() - measure.timeStamp
measure.paused = true
}
return measure.value
}

Timer.prototype.measureLap = function (label) {
return +new Date() - this._.measures[label || ''].timeStamp
}

Timer.prototype.measureStop = function (label) {
return +new Date - this._.measures[label || '']
value = this._.measures[label || ''].value
delete this._.measures[label || '']
return value
}

Timer.prototype.getTime = function (label) {
return this._.measures[label || ''].value
}

function end () {
Expand Down