3

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.

1
  • 2
    If console.time was asynchronous you couldn't measure the time elapsed between console.time and console.timeEnd because it would be impossible to know the begining of your code snippet execution. So it's necessarily synchronous Commented Jun 15, 2015 at 15:06

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.