-
-
Notifications
You must be signed in to change notification settings - Fork 30
Conversation
lib/timing.js
Outdated
*/ | ||
start () { | ||
// once Node 10 is no longer supported, this check can be removed | ||
this.startTime = _.isFunction(process.hrtime.bigint) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
startTime
could be memoized during module load so it doesn't need to happen every time this class is used. Probably a very small optimization, but presumably when timing things we want to have as little overhead as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and by startTime
, i mean, the function used to get the start time
67d4663
to
e4ac9af
Compare
Added the ability to just print the const timer = new timing.Timer().start();
// ...
log.debug(`The operation took ${timer}`); outputs
|
lib/timing.js
Outdated
/** | ||
* Creates a timer | ||
* | ||
* @param {?string} units - the default units for conversion |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still cannot understand why units are needed. We could simply keep the absolute number of seconds/nanoseconds and have asSeconds, asNanos, etc. methods like TimeSpan in C# or Duration in Java have
Alright. Not sure why it's preferable, but moved the conversions in the const timer = new Timer().start();
// ...
log.debug(`This took ${timer.getDuration().asSeconds.toFixed(3)}s`) |
lib/timing.js
Outdated
* Class representing a duration, encapsulating the number and units. | ||
*/ | ||
class Duration { | ||
constructor (duration) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I's rather rename the property to nanos
lib/timing.js
Outdated
this._duration = duration; | ||
} | ||
|
||
get duration () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't this getter redundant now?
Basically. But mirroring the internal, read-only variable. |
lib/timing.js
Outdated
/** | ||
* Get the duration since the timer was started | ||
* | ||
* @return {Duration} the duration, in the specified units |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the param description should be updated
if (_.isArray(this.startTime)) { | ||
// startTime was created using process.hrtime() | ||
const [seconds, nanos] = process.hrtime(this.startTime); | ||
nanoDuration = seconds * NS_PER_S + nanos; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we can return immediately here. There is no need for the nanoDuration
intermediate variable
/** | ||
* Class representing a duration, encapsulating the number and units. | ||
*/ | ||
class Duration { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future we could make the Duration class more advanced (e.g. add +/- operations, factory methods to create duration objects from different units (aka Duration.fromSeconds(x)
, etc)) and use it in other scenarios.
Or use the one, from moment.js lib.
Although, the current implementation is pretty enough for the task it is supposed to solve
We have a number of different mechanisms for getting the duration of operations. This is a first pass at making an extensible API for our use. Once the Performance API is out of super-experimental in Node, this could encapsulate that too, without needing application code to be messed with.