title: Searching Large Array Performance in Swift
body: <p>I have a a JSON file of all the airports in the world that I am trying to search in a function but it is very slow and am trying to improve its performance. There are 9500 entries in this JSON file( I would have used an web API but couldnt find a free one so I am using this JSON file). My array is like like:</p>
  
  <pre><code>var data = [Dictionary&lt;String, AnyObject&gt;]
  </code></pre>
  
  <p>Here is what one of the dictionaries looks like:</p>
  
  <pre><code>[DisplayText: YYZ, airportObject: {
      0 = 193;
      Altitude = 569;
      City = Toronto;
      Country = Canada;
      DST = A;
      IATA = YYZ;
      ICAO = CYYZ;
      Latitude = "43.677223";
      Longitude = "-79.630556";
      Name = "Lester B Pearson Intl";
      TZ = "America/Toronto";
      UTC = "-5";
  }, DisplaySubText: Lester B Pearson Intl]
  </code></pre>
  
  <p>The goal is to have a TextField autocomplete with the name of the airport as the user types in the field. I wrote a function to filter these entries with the user's input. It works, however it is very slow and takes about 1 second per letter typed, and the cpu goes to 50% when typing. </p>
  
  <p>Here is the function </p>
  
  <pre><code>func applyFilterWithSearchQuery(filter : String) -&gt; [Dictionary&lt;String, AnyObject&gt;]
  {
      //let predicate = NSPredicate(format: "DisplayText BEGINSWITH[cd] \(filter)")
      var lower = (filter as NSString).lowercaseString
      var filteredData = data.filter({
              if let match : AnyObject  = $0["DisplayText"]{
                  //println("LCS = \(filter.lowercaseString)")
                  return (match as NSString).lowercaseString.hasPrefix((filter as NSString).lowercaseString)
              }
              else{
                  return false
              }
          })
      return filteredData
  }
  </code></pre>
  
  <p>How can I improve the performance of this function? </p>