title: Improving Twitter's typeahead.js performance with remote data using Django
body: <p>I have a database with roughly 1.2M names. I'm using Twitter's typeahead.js to remotely fetch  the autocomplete suggestions when you type someone's name. In my local environment this takes roughly 1-2 seconds for the results to appear after you stop typing (the autocomplete doesn't appear while you are typing), and 2-5+ seconds on the deployed app on Heroku (using only 1 dyno). </p>
  
  <p>I'm wondering if the reason why it only shows the suggestions after you stop typing (and a few seconds delay) is because my code isn't as optimized?</p>
  
  <p>The script on the page:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  $(document).ready(function() {
    $("#navPersonSearch").typeahead({
      name: 'people',
      remote: 'name_autocomplete/?q=%QUERY'
    })
      .keydown(function(e) {
          if (e.keyCode === 13) {
              $("form").trigger('submit');
          }
      });
  });
  &lt;/script&gt; 
  </code></pre>
  
  <p>The keydown snippet is because without it my form doesn't submit for some reason when pushing enter.</p>
  
  <p>my django view:</p>
  
  <pre><code>def name_autocomplete(request):
      query = request.GET.get('q','')
      if(len(query) &gt; 0):
          results = Person.objects.filter(short__istartswith=query)
          result_list = []
          for item in results:
              result_list.append(item.short)
      else:
          result_list = []
  
      response_text = json.dumps(result_list, separators=(',',':'))
      return HttpResponse(response_text, content_type="application/json")
  </code></pre>
  
  <p>The short field in my Person model is also indexed. Is there a way to improve the performance of my typeahead? </p>