title: Why Groovy's Map scales better than Array?
body: <p>I came into this problemn today and I could not figure out why groovy array is not scaling better than Map when it gets bigger.</p>
  
  <p>In my example I create an Map (LinkedHashMap) and an String array (String[]). Then I iterate from 0 to 10^7 inserting i into the Map or Array. I do it 10 times to be sure that outliers don't mess the results.</p>
  
  <pre><code>int max = 10**7
  int numTests = 10
  
  long totalTimeMap = 0
  long totalTimeArray = 0
  
  numTests.times{
      long start = System.currentTimeMillis()
  
      Map m = [:]
      max.times {
          m[it] = "${it}"
      }
  
      long end = System.currentTimeMillis()
      totalTimeMap += (end-start)
  }
  
  numTests.times {
      long start = System.currentTimeMillis()
  
      String[] s = new String[max]
      max.times{
          s[it] = "${it}"
      }
  
      long end = System.currentTimeMillis()
      totalTimeArray += (end-start)
  }
  
  println "Map: ${totalTimeMap}"
  println "Array: ${totalTimeArray}"
  </code></pre>
  
  <p>The output was unexpected since the Map had a better performance then Array:</p>
  
  <pre><code>Map: 49361
  Array: 101123
  </code></pre>
  
  <p>I made the same experiment in java:</p>
  
  <pre><code>public static void main(String[] args) {
  
          int max = 10000000;
          int numTests = 10;
  
          long totalTimeMap = 0;
          long totalTimeArray = 0;
  
          for(int i=0; i&lt;numTests; i++){
              long start = System.currentTimeMillis();
  
              Map m = new LinkedHashMap();
              for(int j=0; j&lt;max; j++){
                  m.put(j, "" + j);
              }
  
              long end = System.currentTimeMillis();
              totalTimeMap += (end-start);
          }
  
          for(int i=0; i&lt;numTests; i++){
              long start = System.currentTimeMillis();
  
              String[] s = new String[max];
              for(int j=0; j&lt;max; j++){
                  s[j] = "" + j;
              }
  
              long end = System.currentTimeMillis();
              totalTimeArray += (end-start);
          }
  
          System.out.println("Map: " + totalTimeMap);
          System.out.println("Array: " + totalTimeArray);
      }
  </code></pre>
  
  <p>and the output was expected (Array faster than Map):</p>
  
  <pre><code>Map: 34564
  Array: 12822
  </code></pre>
  
  <p>My question is: why the Map is faster than the Array when using Groovy?</p>