7

How to check the number of seconds(or ms) spent inside the particular loop in javascript. I have a sorting algo implemented in javascript , now I am using bubble sort , I want to use quick sort. I know in terms of time efficiency Quick sort is good. But I want to calculate the real number of sec or milli sec spent inside the innermost loop. How do I do in javascript ?

1
  • Addition to all the answers , one more useful functionality I found in firebug is "Timing and profiling" , getfirebug.com/logging , helps to determine JavaScript performance. Commented Oct 18, 2010 at 8:34

5 Answers 5

9

All other answers in this thread are old.

Use this now, it's standard https://developer.mozilla.org/en-US/docs/Web/API/Performance.now

var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")
Sign up to request clarification or add additional context in comments.

Comments

8

The simplest method is to compare by Date.

var old_time = new Date();
...
var new_time = new Date();
var seconds_passed = new_time - old_time;

By the way, why don't you just use the built-in .sort() (https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/Array/sort) method?

4 Comments

ok.Thanks I am not sorting numbers , I am sorting array of objects where I sort depending on one of the property of the object. so built-in sort I cannot use.
It's worth keeping in mind that the Date object is not terribly accurate. John Resig wrote an article about this: ejohn.org/blog/accuracy-of-javascript-time.
@sat: The sort method allows you to specify a compareFunction, which allows you to do exactly what you're after (without having to go off and create your own sorting method). See: developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Objects/… for more information.
This answer is out of date. Check my full answer below.
5

Time is not really accurate on most browsers, you can expect a margin of error about of 15ms:

var start = (new Date).getTime();
/* Your code. */
var diff = (new Date).getTime() - start;

Recommended read:

1 Comment

That was a very good article !! Thanks . Never thought about it.
2

Others have already answered how to do the time calculation, so I'll reply to your comment: "I am sorting array of objects where I sort depending on one of the property of the object. so built-in sort I cannot use."

That's not true at all, you can still use the built in sort:

var arr = [{ text: 'test', id: 2 }, { text: 'abc', id: 6 }, { text: 'xyz', id: 4 }];
arr.sort(function(x,y) { return x.text > y.text ? 1 : x.text < y.text ? -1 : 0 });

4 Comments

So in the above piece of code , its comparing the text property of 2 objects ? What algo is used internally ? means will it compare each n every object or some better algo ?
@sat: For Firefox, merge sort. This is used instead of quick sort to ensure the sort order is stable. See bugzilla.mozilla.org/show_bug.cgi?id=224128.
@David: arr.sort(function(x,y) { return x.text > y.text ? 1 : x.text < y.text ? -1 : 0 });.
@Kenny Thanks for that great info.U get to learn lot of things with simple questions.
2

If you're using Firebug you can do

console.time('someNameHere');
// Do things here
console.timeEnd('someNameHere');

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.