views:

60

answers:

2

I have been writing my first jQuery plugin and struggling to find a means to time how long different pieces of code take to run.

I can use firebug and console.time/profile. However, it seems that because my code executes so fast I get no results with profile and with time it spits out 0ms. (http://stackoverflow.com/questions/2690697/firebug-profiling-issue-no-activity-to-profile/2690846#2690846)

Is there a way to get the time at a greater level of detail that milliseconds in javascript?

UPDATE: I've put code that I want to test in a loop that loops it a million times, but it's not an ideal solution.

A: 
var startTime = new Date();
// do something
var totalTime = new Date() - startTime;
alert(totalTime);
Oleg
This will only do milliseconds. So in the case of the code I want to test it will take 0ms.
Alistair
You can add a loop with 1000 calls to solve the problem, but in general you should optimize only code fragments, which are really slow and not which executing time is less then 1ms. In the case you spend million times more your and our time for this question.
Oleg
+1  A: 

You need to run your tests a number of times and then calculate how many operations per second are executed.

The easiest way to create such a test case is to use the jsPerf website. It will generate a test case like this: http://jsperf.com/prime-numbers

Mathias Bynens