var start = new Date().getTime();
for (i = 0; i < 50000; ++i) {// do something}
var end = new Date().getTime();var time = end - start;alert('Execution time: ' + time);
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`)
console.timeEndValue = function(label) { // Add console.timeEndValue, to add a return valuevar time = this._times[label];if (!time) {throw new Error('No such label: ' + label);}var duration = Date.now() - time;return duration;};
现在像这样使用代码:
console.time('someFunction timer');
someFunction();
var executionTime = console.timeEndValue('someFunction timer');console.log("The execution time is " + executionTime);
var StopWatch = function (performance) {this.startTime = 0;this.stopTime = 0;this.running = false;this.performance = performance === false ? false : !!window.performance;};
StopWatch.prototype.currentTime = function () {return this.performance ? window.performance.now() : new Date().getTime();};
StopWatch.prototype.start = function () {this.startTime = this.currentTime();this.running = true;};
StopWatch.prototype.stop = function () {this.stopTime = this.currentTime();this.running = false;};
StopWatch.prototype.getElapsedMilliseconds = function () {if (this.running) {this.stopTime = this.currentTime();}
return this.stopTime - this.startTime;};
StopWatch.prototype.getElapsedSeconds = function () {return this.getElapsedMilliseconds() / 1000;};
StopWatch.prototype.printElapsed = function (name) {var currentName = name || 'Elapsed:';
console.log(currentName, '[' + this.getElapsedMilliseconds() + 'ms]', '[' + this.getElapsedSeconds() + 's]');};
基准
var stopwatch = new StopWatch();stopwatch.start();
for (var index = 0; index < 100; index++) {stopwatch.printElapsed('Instance[' + index + ']');}
stopwatch.stop();
stopwatch.printElapsed();
function a() {window.performance.mark("start");... do stuff ...window.performance.measure("myfunctionduration", "start");}
// duration is window.performance.getEntriesByName("myfunctionduration", "measure")[0];
var start = new Date().getTime();
for (i = 0; i < 50000; ++i) {// JavaScript is not waiting until the for is finished !!}
var end = new Date().getTime();var time = end - start;alert('Execution time: ' + time);
for的执行可能非常快,所以你看不到结果是错误的。你可以使用执行一些请求的代码来测试它:
var start = new Date().getTime();
for (i = 0; i < 50000; ++i) {$.ajax({url: 'www.oneOfYourWebsites.com',success: function(){console.log("success");}});}
var end = new Date().getTime();var time = end - start;alert('Execution time: ' + time);
var t0 = process.hrtime();//Start of code to measure
//End of codevar timeInMilliseconds = process.hrtime(t0)[1]/1000000; // dividing by 1000000 gives milliseconds from nanoseconds
function functionTimer() {performance.mark('start')functionToBeTimed()performance.mark('end')performance.measure('Start to End', 'start', 'end')const measure = performance.getEntriesByName('Start to End')[0]console.log(measure.duration)}
let test = () => { /* does something */ }test = timed(test) // turns the function into a timed function in one linetest() // 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;}
console.time('function');//run the function in between these two lines for that you need to//measure time taken by the function. ("ex. function();")console.timeEnd('function');
this is the most efficient way :using performance.now(), e.g.
var v1 = performance.now();//run the function here for which you have top measure the timevar v2 = performance.now();console.log("total time taken = "+(v2-v1)+"milliseconds");
use +(add operator) or getTime()
var h2 = +new Date(); //orvar h2 = new Date().getTime();for(i=0;i<500;i++) { /* do something */}var h3 = +new Date(); //orvar h3 = new Date().getTime();var timeTaken = h3-h2;console.log("time ====", timeTaken);
Here's what happens when you apply the unary plus operator to a Date instance:Get the value of the Date instance in questionConvert it to a Number
NOTE: getTime() gives better performance than unary + operator.
var time0 = performance.now(); // Store the time at this point into time0
yourFunction(); // The function you're measuring time for
var time1 = performance.now(); // Store the time at this point into time1
console.log("youFunction took " + (time1 - time0) + " milliseconds to execute");
使用console.time
console.time('someFunction');
someFunction(); // Whatever is timed goes between the two "console.time"
console.timeEnd('someFunction');