I am trying to log time for something. The general code looks like so:
var stream = db.call.stream();
stream.on('data', function () {
if (first) {
console.time('doSomething');
}
stream.pause();
doSomethingWithData(data);
if (stopCondition) {
console.timeEnd('doSomething');
done();
} else {
stream.resume();
}
});
I would like to know if the call to console.time
is blocking or asynchronous? I could not find this in the docs.
答案 0 :(得分:5)
According to the source code of console.time
and console.timeEnd
,
Console.prototype.time = function(label) {
this._times[label] = Date.now();
};
Console.prototype.timeEnd = function(label) {
var time = this._times[label];
if (!time) {
throw new Error('No such label: ' + label);
}
var duration = Date.now() - time;
this.log('%s: %dms', label, duration);
};
They just store the starting time against the label and calculate the time elapsed since the label timed.
They don't do anything asynchronously.
Note: In node.js, if a function is asynchronous, it will accept callback
as one of the parameters.