Is there a way I can check how long it takes for javascript to execute a function in milliseconds?

只愿长相守 提交于 2021-01-28 06:34:44

问题


I have code like the following:

oTable = $('#dataTable').dataTable({
    "sScrollX": "100%",
    "bScrollCollapse": true,
    iDisplayLength: -1,
    aLengthMenu: [[-1, 25, 50, 200, "All"], [10, 25, 50, 200, "All"]],
    aoColumnDefs: [
        { "sSortDataType":"dom-data-rk", "aTargets": ["sort-data-rk"] },
        { "sType": "date-uk", "aTargets": ["sort-date-uk"] },
        { "sType": "datetime-uk", "aTargets": ["sort-datetime-uk"] }
    ]
});

Is there a way I can time how long this code takes to execute using javascript or jQuery. Something like the Stopwatch method in C#?


回答1:


You can use console time and verify how long it takes.

console.time('profile');

for ( var i=0; i < 100000; i++) {
   var arr = new Array();
}

var time = console.timeEnd('profile');

the variable time will have the result in miliseconds example: http://jsfiddle.net/dadviegas/dV9rf/

for all browsers

var one=new Date();
for ( var i=0; i < 100000; i++) {
  var arr = new Array();
}
var two=new Date();

//Calculate difference btw the two dates
alert(two.getMilliseconds()-one.getMilliseconds());

i have added this solution in jsfiddle




回答2:


Try this:

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

// do whatever you want, your code

var end = new Date();
var endTime= end.getTime();
var timeTaken = end - start;
alert('Execution time is : ' + timeTaken);



回答3:


Is there a Date Object

var t = new Date;
//some processing here


console.log( new Date - t ); 



回答4:


for IE, you need to use something like this

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

    oTable = $('#dataTable').dataTable({
     "sScrollX": "100%",
     "bScrollCollapse": true,
     iDisplayLength: -1,
     aLengthMenu: [[-1, 25, 50, 200, "All"], [10, 25, 50, 200, "All"]],
     aoColumnDefs: [
        { "sSortDataType":"dom-data-rk", "aTargets": ["sort-data-rk"] },
        { "sType": "date-uk", "aTargets": ["sort-date-uk"] },
        { "sType": "datetime-uk", "aTargets": ["sort-datetime-uk"] }
     ]
    });

    var end = new Date().getTime();
    var time = end - start;
    alert('Execution time: ' + time);



回答5:


Here's an implementation of the key features of C# Stopwatch. It uses the new performance.now() function -- which is more accurate than getTime() -- where available.

var Stopwatch = function () {
    var startTime;
    var hasPerformance = window.performance && window.performance.webkitNow;

    return {
        start: function () {
            startTime = hasPerformance ? window.performance.webkitNow() : new Date().getTime();
        },
        end: function () {
            this.elapsed = (hasPerformance ? window.performance.webkitNow() : new Date().getTime()) - startTime;
        },
        elapsed: 0
    };
}

Usage:

var myStopwatch = Stopwatch();
myStopwatch.start();
/* code you want to time goes here */
myStopwatch.end()
alert("It took " + stopwatch.elapsed + " milliseconds!");



回答6:


The builtin JS timers do not give good accuracy at all.

I love dynatrace AJAX edition. It is an IE plugin that gives very good timings.

http://ejohn.org/blog/deep-tracing-of-internet-explorer/



来源:https://stackoverflow.com/questions/12163507/is-there-a-way-i-can-check-how-long-it-takes-for-javascript-to-execute-a-functio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!