简体   繁体   English

node.time中的console.time()安全吗?

[英]Is console.time() safe in node.js?

I have a little snippet of node.js code in front of me that looks like this: 我面前有一小段node.js代码,如下所示:

console.time("queryTime");
doAsyncIOBoundThing(function(err, results) {
    console.timeEnd("queryTime");
    // Process the results...
});

And of course when I run this on my (otherwise idle) development system, I get a nice console message like this: 当然,当我在我的(否则是空闲的)开发系统上运行它时,我得到一个很好的控制台消息,如下所示:

queryTime: 564ms

However, if I put this into production, won't there likely be several async calls in progress simultaneously, and each of them will overwrite the previous timer? 但是,如果我把它投入生产,那么同时进行的异步调用是否会有多次,并且每个异步调用都会覆盖以前的计时器? Or does node have some sort of magical execution context that gives each "thread of execution" a separate console timer namespace? 或节点是否具有某种神奇的执行上下文,为每个“执行线程”提供一个单独的控制台计时器命名空间?

Just use unique labels and it will be safe. 只需使用独特的标签即可。 That's why you use a label, to uniquely identify the start time. 这就是您使用标签来唯一标识开始时间的原因。

As long as you don't accidentally use a label twice everything will work exactly as intended. 只要您不小心使用标签两次,一切都将完全按预期工作。 Also note that node has usually only one thread of execution. 另请注意, node通常只有一个执行线程。

Wouldn't this simple code work? 这个简单的代码不会起作用吗?

var labelWithTime = "label " + Date.now();
console.time(labelWithTime);
// Do something
console.timeEnd(labelWithTime);

Consider new NodeJS features as it has evolved too. 考虑新的NodeJS功能,因为它已经发展。 Please look into: 请看看:

process.hrtime() & NodeJS's other performance API hooks: process.hrtime()和NodeJS的其他性能API挂钩:

https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_timing_api https://nodejs.org/api/perf_hooks.html#perf_hooks_performance_timing_api

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM