How to time the time until a function finishes? [duplicate]

1

I want to time the whole program to finish its process, I need that time for comparison. Is there any easy way to implement this?

    
asked by anonymous 26.04.2015 / 05:19

1 answer

0

Do this:

console.time('teste');
for (var i = 0; i < 10000; i++) {
    // seu código aqui
}
console.timeEnd('teste');

In this case, your code will run 10000 times and in the end you will know how much time has passed. Very useful for comparing codes to know which is the most efficient, p. ex:

console.time('teste1');
for (var i = 0; i < 10000; i++) {
    $('#painel .lista');
}
console.timeEnd('teste1');


console.time('teste2');
for (var i = 0; i < 10000; i++) {
    $('#painel').find('.lista');
}
console.timeEnd('teste2');

In this example, you find out which snippet is most efficient: $('#painel .lista') or $('#painel').find('.lista') , using jQuery.

See also: link

    
26.04.2015 / 06:22