Determing Effect of Optimization
<p>Disclaimer: This question uses jQuery for an example, but is not about jQuery.</p>

<p>Is there a way to determine how the compiler optimizes a certain piece of code?</p>

<p>For instance, assuming <code>someCollection.size()</code> is invariant, this code:</p>

<pre><code>for (var i = 0; i &lt; someCollection.size(); i++) ...
</code></pre>

<p>In more recent compilers, should get optimized to something more like this:</p>

<pre><code>var _unnamed = someCollection.size();
for (var i = 0; i &lt; _unnamed; i++) ...
</code></pre>

<p>So writing this code yourself gives you no value (and may be less readable).</p>

<p>Recently, I wanted to see how using arrow functions would affect performance.  Theoretically, they could be faster than the equivalent normal function since they have less overhead (they don't bind a number of things).  I used this as a test case:</p>

<pre><code>$('.someClass').each( (undefined, value) =&gt; { doSomething($(value)); } );
</code></pre>

<p>And an equivalent using a "normal" function:</p>

<pre><code>$('.someClass').each( function() { doSomething($(this)); } );
</code></pre>

<p>Each method was run a few thousand times, first the arrow function and then the normal function and I was surprised to see that the arrow function was consistently slightly slower.  To be fair, I then ran the normal function first and the result was reversed, the normal function was slower.  It didn't seem to matter which function was run first - it would always be slower.  Once I started arbitrarily running one method of the function first and throwing away those results the rest of the results became very similar.</p>

<p>My assumption is that there is some optimization being done in the first test that is affecting the results of the subsequent tests.</p>

<p>My question is: How can I find out what the optimizer is doing so I can determine if any particular optimization actually has an effect?</p>