Is console.time() safe in node.js?

前端 未结 3 1513
半阙折子戏
半阙折子戏 2021-02-01 06:40

I have a little snippet of node.js code in front of me that looks like this:

console.time(\"queryTime\");
doAsyncIOBoundThing(function(err, results) {
    consol         


        
相关标签:
3条回答
  • 2021-02-01 07:17

    Wouldn't this simple code work?

    var labelWithTime = "label " + Date.now();
    console.time(labelWithTime);
    // Do something
    console.timeEnd(labelWithTime);
    
    0 讨论(0)
  • 2021-02-01 07:23

    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.

    0 讨论(0)
  • 2021-02-01 07:34

    Consider new NodeJS features as it has evolved too. Please look into:

    process.hrtime() & NodeJS's other performance API hooks:

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

    0 讨论(0)
提交回复
热议问题