High resolution in Stopwatch #6

Closed
wants to merge 2 commits into
from

Projects

None yet

2 participants

@seriousManual

Adressing #5
Stopwatch measures time using the highResolution timer with a (potential) resolution of 1ns.

@felixge felixge commented on the diff Jul 18, 2012
lib/util/Stopwatch.js
this._ended = false;
}
+Stopwatch.prototype._getTime = function() {
+ if (!process.hrtime) {
+ return Date.now;
+ } else {
+ return function() {
+ var hrtime = process.hrtime();
+ return hrtime[0] / 1000 + hrtime[1] / (1000 * 1000);
+ }
+ }
+}();
+
@felixge
felixge Jul 18, 2012

I don't think this can work. You should return the time, not a time function here.

@seriousManual
seriousManual Jul 18, 2012

the return of the time function only happens one time when the module gets required.
so after require('Stopwatch.js') it's either Stopwatch.prototype._getTime = Date.now or Stopwatch.prototype._getTime = function() { var hrtime..... } depending if process.hrtime is available or not.

i did it this way to not have decide if date.now or hrtime is to use when Stopwatch gets instantiated. (which can be lots of times, depending on the use case)

@oliverzy oliverzy referenced this pull request Mar 19, 2014
Merged

High Resolution Timer #10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment