How do I measure the execution time of JavaScript code with callbacks?

2021年11月20日 阅读数:2
这篇文章主要向大家介绍How do I measure the execution time of JavaScript code with callbacks?,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

问题:

I have a piece of JavaScript code that I am executing using the node.js interpreter.我有一段使用node.js解释器执行的 JavaScript 代码。node

for(var i = 1; i < LIMIT; i++) {
  var user = {
    id: i,
    name: "MongoUser [" + i + "]"
  };
  db.users.save(user, function(err, saved) {
    if(err || !saved) {
      console.log("Error");
    } else {
      console.log("Saved");
    }
  });
}

How can I measure the time taken by these database insert operations?如何测量这些数据库插入操做所花费的时间? I could compute the difference of date values after and before this piece of code but that would be incorrect because of the asynchronous nature of the code.我能够计算这段代码先后日期值的差别,但因为代码的异步性质,这是不正确的。数据库


解决方案:

参考: https://stackoom.com/en/question/iXz4