1

I need to compare the computation speed of some code, but I'm not sure what the best way to do that is. Is there some kind of built-in timer to do this? And is it possible to get the computation speed in nanoseconds, or do I need to deal with the milliseconds JavaScript usually works with?

Joeytje50
  • 18,636
  • 15
  • 63
  • 95

6 Answers6

2

I came across the performance API. What you're looking for is probably Performance.now(), which will give you microsecond percision.

The Performance.now() method returns a DOMHighResTimeStamp, measured in milliseconds, accurate to one thousandth of a millisecond equal to the number of milliseconds since the PerformanceTiming.navigationStart property and the call to the method (source).

The example MDN provides is:

var t0 = performance.now();
doSomething();
var t1 = performance.now();
console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.")

A function you could use to test the performance of a specific short piece of code multiple times, you could use the following:

/**
 * Finds the performance for a given function
 * function fn the function to be executed
 * int n the amount of times to repeat
 * return array [time elapsed for n iterations, average execution frequency (executions per second)]
 */
function getPerf(fn, n) {
  var t0, t1;
  t0 = performance.now();
  for (var i = 0; i < n; i++) {
    fn(i)
  }
  t1 = performance.now();
  return [t1 - t0, repeat * 1000 / (t1 - t0)];
}

Which returns the amount of time a single execution of fn(i) takes in milliseconds, and the frequency of execution (executions per second). The higher the n value, the more precision this has, but the longer it takes to test. The argument i can be included in the function to be tested, which contains the current iteration of the function.

Joeytje50
  • 18,636
  • 15
  • 63
  • 95
jeremy
  • 9,965
  • 4
  • 39
  • 59
1

You may be looking for console.time. See https://developer.mozilla.org/en-US/docs/Web/API/console?&redirectslug=DOM%2Fconsole#Timers.

  • This is still in milliseconds, but this looks as much of a built-in debugging function as it would get, so I guess I'll have to deal with the milliseconds and just run the loop for a bit more iterations. – Joeytje50 Dec 29 '13 at 04:11
1

You should use jsPerf to get statistically significant results. Due to the various JIT systems involved in browsers, performance results can vary a lot, so jsPerf or the Benchmarking.js library it uses will run your code as many times as it needs to get good results. But note that if a function is running in just a few nanoseconds worrying about its performance is almost always unnecessary.

curiousdannii
  • 1,658
  • 1
  • 25
  • 40
0

You can use the Developer Tools Script Profiler of Internet explorer. I am sure there is a similar tool for Chrome and Firefox too. It will give you in milliseconds. But if the time taken is less than a millisecond then I think it will show in nanoseconds too.

Aninda Sen
  • 63
  • 1
  • 7
0

You could try the profiling function built-into browser:

They are rich-feature and can be used out of the box without writing a single line of code

Khanh TO
  • 48,509
  • 13
  • 99
  • 115
0

An additional tool is https://github.com/anywhichway/benchtest which just re-uses Mocha unit tests.

AnyWhichWay
  • 716
  • 8
  • 11