Javascript 如何测量脚本的执行时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4784745/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:04:53  来源:igfitidea点击:

How can I measure the execution time of a script?

javascripttimer

提问by Skizit

How would I measure the time it takes for a script to run from start to finish?

我将如何衡量脚本从头到尾运行所需的时间?

 start-timing
   //CODE
 end-timing

回答by Arnaud Le Blanc

EDIT: in January 2011, this was the best available solution. Other solutions (such as performance.now()should be preferred now.

编辑:2011 年 1 月,这是最好的可用解决方案。其他解决方案(例如performance.now()现在应该首选。

var start = new Date();
    // CODE
var time = new Date() - start;
// time is the number of milliseconds it taken to execute the script

You may also want to wrap that in a function:

您可能还想将其包装在一个函数中:

function time_my_script(script) {
    var start = new Date();
    script();
    return new Date() - start;
}

// call it like this:
time = time_my_script(function() {
    // CODE
});

// or just like this:
time = time_my_script(func);

If you are trying to profile your code, you may want to try the Firebugextension, which includes a javascript profiler. It has a great user interface for profiling, but it also can be done programmatically with its console api :

如果您正在尝试分析您的代码,您可能想要尝试Firebug扩展,它包括一个 javascript 分析器。它具有用于分析的出色用户界面,但也可以使用其控制台 api以编程方式完成:

console.time('timer1');
    // CODE
console.timeEnd('timer1'); // this prints times on the console

console.profile('profile1');
    // CODE
console.profileEnd('profile1'); // this prints usual profiling informations, per function, etc.

回答by Morteza Ziyae

Use performance.now()instead of new Date(). This provides a more accurate and better result. See this answer https://stackoverflow.com/a/15641427/730000

使用performance.now()代替new Date()。这提供了更准确和更好的结果。请参阅此答案https://stackoverflow.com/a/15641427/730000

回答by Steven Kaspar

Here is a quick function to work like a stopwatch

这是一个像秒表一样工作的快速功能

var Timer = function(id){
  var self = this;
  self.id  = id;
  var _times = [];
  self.start = function(){
    var time = performance.now();
    console.log('[' + id + '] Start');
    _times.push(time);
  }
  self.lap = function(time){
    time = time ? time: performance.now();
    console.log('[' + id + '] Lap ' + time - _times[_times.length - 1]);
    _times.push(time);
  }
  self.stop = function(){
    var time = performance.now();
    if(_times.length > 1){
      self.lap(time);
    }
    console.log('[' + id + '] Stop ' + (time - _times[0]));
    _times = [];
  }
}
// called with
var timer = new Timer('process label');
timer.start(); // logs => '[process label] Start'
// ... code ...
timer.lap();   // logs => '[process label] Lap ' + lap_time
// ... code ...
timer.stop();  // logs => '[process label] Stop ' + start_stop_diff

回答by adardesign

Use the User Timing api

使用用户计时 api

For example: in the beginningof the JS file write: performance.mark("start-script")

例如:在JS文件的开头写:performance.mark("start-script")

at the endof the JS file write: performance.mark("end-script")

结束JS文件写的:performance.mark("end-script")

then you can measure it too:

那么你也可以测量它:

performance.measure("total-script-execution-time", "start-script", "end-script");

performance.measure("total-script-execution-time", "start-script", "end-script");

This will give you the time it takes to run the entire script execution.

这将为您提供运行整个脚本执行所需的时间。