我需要以毫秒为单位获取执行时间。

我最初问这个问题是在2008年。当时接受的答案是使用newDate().getTime()。然而,我们现在都可以同意使用标准performance.now()API更合适。因此,我将接受的答案改为这个答案。


当前回答

由于console.time和performance.now在一些主要浏览器(如IE10)中不受支持,我创建了一个利用最佳可用方法的瘦实用程序。然而,它缺少错误用法的错误处理(在未初始化的计时器上调用End())。

使用它并根据您的需要进行改进。

Performance: {
    Timer: {},
    Start: function (name) {
        if (console && console.time) {
            console.time(name);
        } else if (window.performance.now) {
            this.Timer[name] = window.performance.now();
        } else {
            this.Timer[name] = new Date().getTime();
        }
    },
    End: function (name) {
        if (console && console.time) {
            console.timeEnd(name);
        } else {
            var result;
            if (window.performance.now) {
                result = window.performance.now() - this.Timer[name];
            } else {
                result = new Date().getTime() - this.Timer[name];
            }
            console.log(name + ": " + result);
        }
    }
}

其他回答

使用performance.now():

var startTime = performance.now()

doSomething()   // <---- measured code goes between startTime and endTime
    
var endTime = performance.now()

console.log(`Call to doSomething took ${endTime - startTime} milliseconds`)

在Node.js中,需要导入性能类

进口性能

const { performance } = require('perf_hooks');

使用console.time:(生活水平)

console.time('doSomething')
    
doSomething()   // <---- The function you're measuring time for 
    
console.timeEnd('doSomething')

注意:传递给time()和timeEnd()方法的字符串必须匹配(计时器才能按预期完成)。

console.time()文档:MDN文档Node.js文档

下面是计时函数的修饰符

它包装函数,以便它们每次运行时都能计时

用法:

let test = () => { /* does something */ }
test = timed(test)   // turns the function into a timed function in one line
test()               // run your code as normal, logs 'function test took 1001.900ms' 

这是装饰者:

let timed = (f) => (...args) => {
    let start = performance.now();
    let ret = f(...args);
    console.log(`function ${f.name} took ${(performance.now() - start).toFixed(3)}ms`);
    return ret;   
}

如果您使用的是异步函数,您可以将其设置为定时异步,并在f(…args)之前添加一个await,这应该适用于这些函数。如果您希望一个装饰器同时处理同步和异步函数,则会变得更加复杂。

有多种方法可以实现这一目标:

使用console.timeconsole.time(“函数”);//在这两行之间运行函数//测量函数所用的时间。(“例如函数();”)console.timeEnd('功能');这是最有效的方法:使用performance.now(),例如。var v1=performance.now();//在这里运行您可以测量时间的函数var v2=性能.now();console.log(“总时间=”+(v2-v1)+“毫秒”);使用+(add运算符)或getTime()var h2=+新日期()//或var h2=新日期().getTime();对于(i=0;i<500;i++){/*做某事*/}var h3=+新日期()//或var h3=新日期().getTime();var timeTaken=h3-h2;console.log(“time===”,timeTaken);

以下是将一元加号运算符应用于Date实例时的情况:获取相关Date实例的值将其转换为数字

注意:getTime()比一元+运算符提供更好的性能。

在函数之前使用console.time(“此处的某个标签”),在函数之后使用console.timeEnd(“此处某个标签)。它将为您提供函数的运行时间。

由于console.time和performance.now在一些主要浏览器(如IE10)中不受支持,我创建了一个利用最佳可用方法的瘦实用程序。然而,它缺少错误用法的错误处理(在未初始化的计时器上调用End())。

使用它并根据您的需要进行改进。

Performance: {
    Timer: {},
    Start: function (name) {
        if (console && console.time) {
            console.time(name);
        } else if (window.performance.now) {
            this.Timer[name] = window.performance.now();
        } else {
            this.Timer[name] = new Date().getTime();
        }
    },
    End: function (name) {
        if (console && console.time) {
            console.timeEnd(name);
        } else {
            var result;
            if (window.performance.now) {
                result = window.performance.now() - this.Timer[name];
            } else {
                result = new Date().getTime() - this.Timer[name];
            }
            console.log(name + ": " + result);
        }
    }
}