Bug when attempting to time my JavaScript functions
<p>I was given the challenge which is essentially: given an array of numbers as input, find the greatest difference between any two numbers, given that j &lt; i, and input[i] > input[j].</p>

<p>I wrote two solutions, one a brute-force double nested loop and the other my attempt at optimizing it. They are as follows:</p>

<pre><code>// Brute Force Method
function findGreatestDiffV1(nums) {
  let start = new Date().getTime();
  let greatestDiff = 0;
  for (let i = 1; i &lt; nums.length; i++) {
    for (let j = 0; j &lt; i; j++) {
      let diff = nums[i] - nums[j];
      if (diff &gt; greatestDiff) greatestDiff = diff;
    }
  }
  console.log(((new Date().getTime() - start) / 1000.0) + " Seconds");
  return greatestDiff;
}

// Optimized method
function findGreatestDiffV2(nums) {
  let start = new Date().getTime();
  let smallestNum = nums[0];
  let greatestDiff = 0;
  for (let i = 1; i &lt; nums.length; i++) {
    if (nums[i] &lt; smallestNum) smallestNum = nums[i];
    if ((nums[i] - smallestNum) &lt; greatestDiff) continue;
    for (let j = 0; j &lt; i; j++) {
      let diff = nums[i] - nums[j];
      if (diff &gt; greatestDiff) greatestDiff = diff;
    }
  }
  console.log(((new Date().getTime() - start) / 1000.0) + " Seconds");
  return greatestDiff;
}
</code></pre>

<p>These work fine, and output both the correct results as well as the expected run-times, i.e. significantly lower for V2 for larger datasets.</p>

<p>I then modified them to return only the run-times:</p>

<pre><code>function findGreatestDiffV1Time(nums) {
  let start = new Date().getTime();
  let greatestDiff = 0;
  for (let i = 1; i &lt; nums.length; i++) {
    for (let j = 0; j &lt; i; j++) {
      let diff = nums[i] - nums[j];
      if (diff &gt; greatestDiff) greatestDiff = diff;
    }
  }
  return ((new Date().getTime() - start) / 1000.0);
}

function findGreatestDiffV2Time(nums) {
  let start = new Date().getTime();
  let smallestNum = nums[0];
  let greatestDiff = 0;
  for (let i = 1; i &lt; nums.length; i++) {
    if (nums[i] &lt; smallestNum) smallestNum = nums[i];
    if ((nums[i] - smallestNum) &lt; greatestDiff) continue;
    for (let j = 0; j &lt; i; j++) {
      let diff = nums[i] - nums[j];
      if (diff &gt; greatestDiff) greatestDiff = diff;
    }
  }
  return ((new Date().getTime() - start) / 1000.0);
}
</code></pre>

<p>I created this helper function, which generates inputs of length <code>numOfDiffs</code> and of greatest individual number <code>maxDiff</code>:</p>

<pre><code>function generateDiffs(numOfDiffs, maxDiff) {
  let diffs = [];
  for (let i = 0; i &lt; numOfDiffs; i++) {
    diffs.push(Math.floor(Math.random() * maxDiff));
  }
  return diffs;
}
</code></pre>

<p>However, this is the point where things are bugging out and I cannot figure out why.</p>

<p>When using the versions which only return the run-time, regardless of the size of the input, I'm getting a time of 0 for V1. V2 is returning the correct time for any size set, but V1 continues to escape almost immediately and return 0.</p>

<p>The most likely issue is that I have an obvious bug somewhere, but I'm wondering if it might be some misunderstanding of how JavaScript performs some function, or even scope bleed somewhere. But regardless, I can't locate it and it's driving me crazy.</p>