Javascript 如何计算一个函数在 jQuery 中花费的时间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7314162/
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-24 01:45:54  来源:igfitidea点击:

How can I calculate how much time a function takes in jQuery?

javascriptjqueryajax

提问by vorillaz

I have jQuery functions; e.g A(), B() and C() Each function makes some Ajax calls to different sites.

我有 jQuery 函数;例如 A()、B() 和 C() 每个函数都会对不同的站点进行一些 Ajax 调用。

I want to calculate how much time it takes to run each function (I guess in milliseconds) I just want to test my code in long loops and in different modern browsers (Safari/Chrome/IE10/Mozilla). More specifically, I want to calculate how much time it takes to take a callback from each Ajax request (this is also the time that the function ends right?) How might I achieve this?

我想计算运行每个函数所需的时间(我想以毫秒为单位)我只想在长循环和不同的现代浏览器(Safari/Chrome/IE10/Mozilla)中测试我的代码。更具体地说,我想计算从每个 Ajax 请求中获取回调所需的时间(这也是函数结束的时间?)我该如何实现?

回答by Will

You could get the time in milliseconds from the date object.

您可以从日期对象中获取以毫秒为单位的时间。

var start = new Date().getTime();

A();

function AJAXSuccessFunction() {
    console.log(new Date().getTime() - start);
}

回答by ejang

//set start point anywhere you want
var start = new Date(); 
//when done,
var end = new Date();

//to profile milliseconds, just do 
var duration = end - start;

回答by RichardBernards

Nowadays, you can use perfomance.now(). This function uses the navigationStartattribute as a start time;

如今,您可以使用perfomance.now(). 该函数使用该navigationStart属性作为开始时间;

This attribute must return the time immediately after the user agent finishes prompting to unload the previous document. If there is no previous document, this attribute must return the same value as fetchStart.

此属性必须在用户代理完成提示卸载前一个文档后立即返回时间。如果没有先前的文档,则此属性必须返回与 fetchStart 相同的值。

As for a real life code example:

至于现实生活中的代码示例:

// Store the start
var start = performance.now();

// Do some operation
for(var i=0; i < 1000; i++) {
    var j = i*i;
}

// Profile the milliseconds it took to do the job
var duration = (performance.now() - start);

回答by VeeK

I don't know if this is what you're looking for but if you want to know how much time was spent processing a function, you can use Chrome's DevTools (or similar tools in other browsers) to check that.

我不知道这是否是您要查找的内容,但是如果您想知道处理函数所花费的时间,您可以使用 Chrome 的 DevTools(或其他浏览器中的类似工具)来检查。

Go to the page you want to examine, open DevTools, go to Profilestab and select 'Collect Javascript CPU profile' and hit Startto start recording.

转到您要检查的页面,打开 DevTools,转到Profiles选项卡并选择“收集 Javascript CPU 配置文件”并点击Start开始录制。

Refresh your page and when it has completed loading, Stopthe recording. You'll get a list of functions executed and time that was spent processing it.

刷新您的页面,当它完成加载时,Stop记录。您将获得执行的函数列表以及处理它所花费的时间。

Sample Screenshot of Profiles Tab in Dev Tools

开发工具中配置文件选项卡的示例屏幕截图

Hope that helps.

希望有帮助。

回答by Gaurav_soni

You can now use , console.time('fn1') and console.timeEnd('fn1')

您现在可以使用 , console.time('fn1') 和 console.timeEnd('fn1')

So if you are calling a function like doAwesomeStuff() and want to see how much time it takes to execute it just do,

因此,如果您正在调用 doAwesomeStuff() 之类的函数并想查看执行它需要多少时间,请执行以下操作,

console.time('fn1')
doAwesomeStuff()
console.timeEnd('fn1')

fn1 - can be anything.

fn1 - 可以是任何东西。

回答by Nico

You can use more precisely performance.now()

您可以更精确地使用performance.now()

// Take a timestamp at the beginning.
var start = performance.now();

// Execute the code being timed.
console.log("your function is here")

// Take a final timestamp.
var end = performance.now();

// Calculate the time taken and output the result in the console
console.log('doTasks took ' + (end - start) + ' milliseconds to execute.');