Is "console.time" in nodejs synchronous or asynchronous?
I'm trying to write time down for something. The general code looks like this:
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 is console.time
blocking or asynchronous? I couldn't find this in the docs.
+3
source to share
1 answer
According to the source code and , console.time
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 simply store the start time of the tag and calculate the elapsed time since the tag was assigned.
They don't do anything asynchronously.
Note. In node.js, if a function is asynchronous, it takes callback
as one of the parameters.
+5
source to share