title: Filter results, slow performance Swift
body: <p>Swift question...
  I'm making a babyname app and trying to filter the results as chosen by the user. I managed to get it working, but it takes a while for the results to get filtered. I mean like 2-3 seconds.</p>
  <p>Here is what I wrote :</p>
  <pre><code>func apply(list: [String]) -&gt; [String] {
          let allSavedSettings = settings.all
  
          var newList = list
          
          if let short = allSavedSettings[&quot;short&quot;] as? Bool {
              if !short {
                  print(&quot;filter short&quot;)
                  newList = newList.filter({$0.count &gt; 4})
              }
          }
          if let long = allSavedSettings[&quot;long&quot;] as? Bool {
              if !long {
                  print(&quot;filter long&quot;)
  
                  newList = newList.filter({$0.count &lt; 5})
              }
          }
          if let dutch = allSavedSettings[&quot;dutch&quot;] as? Bool {
              if !dutch {
                  print(&quot;filter dutch&quot;)
  
                  newList = newList.filter({!dutchboy.contains($0)})
                  newList = newList.filter({!dutchgirl.contains($0)})
              }
          }
          if let english = allSavedSettings[&quot;english&quot;] as? Bool {
              if !english {
                  print(&quot;filter english&quot;)
  
                  newList = newList.filter({!englishboy.contains($0)})
                  newList = newList.filter({!englishgirl.contains($0)})
                  
              }
          }
          if let arabic = allSavedSettings[&quot;arabic&quot;] as? Bool {
              if !arabic {
                  print(&quot;filter arabic&quot;)
  
                  newList = newList.filter({!arabicboy.contains($0)})
                  newList = newList.filter({!arabicgirl.contains($0)})
                  
              }
          }
          if let hebrew = allSavedSettings[&quot;hebrew&quot;] as? Bool {
              if !hebrew {
                  print(&quot;filter hebrew&quot;)
  
                  newList = newList.filter({!hebrewboy.contains($0)})
                  newList = newList.filter({!hebrewgirl.contains($0)})
                  
              }
          }
          if let latin = allSavedSettings[&quot;latin&quot;] as? Bool {
              if !latin {
                  print(&quot;filter latin&quot;)
  
                  newList = newList.filter({!latinboy.contains($0)})
                  newList = newList.filter({!latingirl.contains($0)})
                  
              }
          }
          if let chinese = allSavedSettings[&quot;chinese&quot;] as? Bool {
              if !chinese {
                  print(&quot;filter chinese&quot;)
  
                  newList = newList.filter({!chineseboy.contains($0)})
                  newList = newList.filter({!chinesegirl.contains($0)})
                  
              }
          }
          if let scandinavian = allSavedSettings[&quot;scandinavian&quot;] as? Bool {
              if !scandinavian {
                  print(&quot;filter scandinavian&quot;)
  
                  newList = newList.filter({!scandinavianboy.contains($0)})
                  newList = newList.filter({!scandinaviangirl.contains($0)})
                  
              }
          }
          if let spanish = allSavedSettings[&quot;spanish&quot;] as? Bool {
              if !spanish {
                  print(&quot;filter spanish&quot;)
  
                  newList = newList.filter({!spanishboy.contains($0)})
                  newList = newList.filter({!spanishgirl.contains($0)})
                  
              }
          }
          
          return newList
      }
  </code></pre>
  <p>So I save the users preferences as a Boolean value in an array called &quot;allSavedSettings&quot; with userdefaults. Whenever a setting is false it will filter the result from the complete list of names.</p>
  <p>Is there something else I should use, to speed things up? The list is about 5000 names.</p>
  <p>Thanks in advance.</p>
  <p>Patrick</p>