title: Elegant way to find contiguous subarray within an array in JavaScript?
body: <p>I wanted to write a function to find a contiguous subarray within a given array from a given starting index and return the index of the subarray within the array if it's found, and -1 if it's not found. This is similar to <code>String.indexOf</code>, but for arrays and subarrays instead of strings and substrings.</p>
  
  <p>This is my working code:</p>
  
  <pre><code>var find_csa = function (arr, subarr, from_index) {
      if (typeof from_index === 'undefined') {
          from_index = 0;
      }
  
      var i, found, j;
      for (i = from_index; i &lt; 1 + (arr.length - subarr.length); ++i) {
          found = true;
          for (j = 0; j &lt; subarr.length; ++j) {
              if (arr[i + j] !== subarr[j]) {
                  found = false;
                  break;
              }
          }
          if (found) return i;
      }
      return -1;
  };
  </code></pre>
  
  <p>And these are my tests and their expected values:</p>
  
  <pre><code>console.log(find_csa([1, 2, 3, 4, 5], [2, 3, 4]) === 1);
  console.log(find_csa([1, 2, 3, 4, 5], [5]) === 4);
  console.log(find_csa([1, 2, 3, 4, 5], [1, 3]) === -1);
  console.log(find_csa([1, 2, 3, 4, 5], [42]) === -1);
  console.log(find_csa([1, 2, 3, 4, 5], []) === 0);
  console.log(find_csa([3, 4, 3, 4, 3, 4], [3, 4, 3], 1) === 2);
  console.log(find_csa([6, 6, 6, 7], [6, 6, 7]) === 1);
  console.log(find_csa([12, 9, 16, 42, 7, 866, 3], [16, 42, 7, 866]) === 2);
  </code></pre>
  
  <p>My code passes the tests, but as you can see, it uses a boolean value <code>found</code> in the inner loop which is just my messy, ad-hoc way of continuing an outer loop from a nested loop. is there a cleaner way of writing it? I looked into <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex" rel="noreferrer"><code>Array.prototype.findIndex</code></a> but it is an experimental technology at the moment so I can't use it. I want a method that works in most browsers. I know there is a "polyfill" code snippet written on the Mozilla page, but that is even longer than my current code and it will be slower due to the function calls, so I'd rather avoid it.</p>
  
  <p>My primary goal for this function is <em>performance</em> (the subarrays will be very small, so I believe that using <a href="http://en.wikipedia.org/wiki/Boyer%E2%80%93Moore_string_search_algorithm" rel="noreferrer">Boyer-Moore string search algorithm</a> or <a href="http://en.wikipedia.org/wiki/Trie" rel="noreferrer">tries</a> is a tad overkill), and then my secondary goal is <em>elegance</em> of my implementation. With those two goals in mind, I would like to know if there is a better way of writing this code, or if there are any JavaScript features or functions that I'm missing that could help me avoid the <code>found</code> boolean.</p>
  
  <p>JSFiddle if it helps anyone: <a href="http://jsfiddle.net/qc4zxq2p/" rel="noreferrer">http://jsfiddle.net/qc4zxq2p/</a></p>