Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I came across the <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.performance" rel="nofollow">performance API</a>. What you're looking for is probably <code>Performance.now()</code>, which will give you microsecond percision.</p> <blockquote> <p>The <strong><code>Performance.now()</code></strong> method returns a <a href="https://developer.mozilla.org/en-US/docs/Web/API/DOMHighResTimeStamp" rel="nofollow">DOMHighResTimeStamp</a>, measured in milliseconds, accurate to one thousandth of a millisecond equal to the number of milliseconds since the <a href="https://developer.mozilla.org/en-US/docs/Web/API/PerformanceTiming.navigationStart" rel="nofollow">PerformanceTiming.navigationStart</a> property and the call to the method <a href="https://developer.mozilla.org/en-US/docs/Web/API/Performance.now" rel="nofollow">(source).</a></p> </blockquote> <p>The example MDN provides is:</p> <pre><code>var t0 = performance.now(); doSomething(); var t1 = performance.now(); console.log("Call to doSomething took " + (t1 - t0) + " milliseconds.") </code></pre> <p>A function you could use to test the performance of a specific short piece of code multiple times, you could use the following:</p> <pre><code>/** * 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 &lt; n; i++) { fn(i) } t1 = performance.now(); return [t1 - t0, repeat * 1000 / (t1 - t0)]; } </code></pre> <p>Which returns the amount of time a single execution of <code>fn(i)</code> takes in milliseconds, and the frequency of execution (executions per second). The higher the <code>n</code> value, the more precision this has, but the longer it takes to test. The argument <code>i</code> can be included in the function to be tested, which contains the current iteration of the function. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload