title: Laravel Slow Response of an Ajax request to a controller method returning JSON Data
body: <p>I am trying to perform an Ajax request from view to a controller which returns JSON Data. Everything is working fine however the Ajax response is too slow.</p>
  
  <p>This is my Ajax Request in view:</p>
  
  <pre><code>$(document).on('keyup', '#search', function () {
      $("#phoneBody &gt; tr").remove();
      var query = $(this).val().toLowerCase();
      var expression = new RegExp(query, "i");
      $.getJSON("phonetest/" + query, function (data) {
          $.each(data, function (key, value) {
              if (value.name.search(expression) != -1 ||
                  value.extn.search(expression) != -1 ||
                  value.mobile.search(expression) != -1 ||
                  value.dept.search(expression) != -1 ||
                  value.desig.search(expression) != -1 ||
                  value.emp_cd.search(expression) != -1)
              {
                  $('#phoneBody').append('&lt;tr&gt;&lt;td class="name"&gt;' + value.name +
                                         '&lt;/td&gt;&lt;td class="extn"&gt;' + value.extn + '&lt;/td&gt;&lt;td&gt;' +
                                         value.email + '&lt;/td&gt;&lt;td&gt;' + value.voip + '&lt;/td&gt;&lt;td&gt;' +
                                         value.mobile + '&lt;/td&gt;&lt;td&gt;' + value.floor + '&lt;/td&gt;&lt;td&gt;' +
                                         value.dept + '&lt;/td&gt;&lt;td&gt;' + value.desig + '&lt;/td&gt;&lt;/tr&gt;');
              }
          });
      });
  
  });
  </code></pre>
  
  <p>This is my controller method:</p>
  
  <pre><code>public function phonetest(Request $request)
  {
      if ($request-&gt;ajax()) {
          $crawler = Goutte::request('GET', "http://1.1.1.1/test.NSF/names?OpenView&amp;count=2000");
  
          $emp_arr = $crawler-&gt;filter('tr')-&gt;each(function ($node) {
              $rec['name']   = $node-&gt;filter('a')-&gt;eq(0)-&gt;text();
              $rec['extn']   = $node-&gt;filter('td')-&gt;eq(1)-&gt;text();
              $rec['voip']   = $node-&gt;filter('td')-&gt;eq(2)-&gt;text();
              $rec['mobile'] = $node-&gt;filter('td')-&gt;eq(3)-&gt;text();
              $rec['floor']  = $node-&gt;filter('td')-&gt;eq(4)-&gt;text();
              $rec['dept']   = $node-&gt;filter('td')-&gt;eq(5)-&gt;text();
              $rec['desig']  = $node-&gt;filter('td')-&gt;eq(6)-&gt;text();
              $rec['emp_cd'] = $node-&gt;filter('td')-&gt;eq(7)-&gt;text();
  
              $email        = DB::table('employee1')
                  -&gt;where('emp1_code', $rec['emp_cd'])
                  -&gt;pluck('emp1_email_addr')
                  -&gt;first();
              $rec['email'] = strtolower($email);
              //dd($rec);
  
              return $rec;
          });
          $emp_arr = array_values(array_filter($emp_arr));
  
          return json_encode($emp_arr);
      }
  }
  </code></pre>
  
  <p>Everything works fine. However, the response is too slow. It takes literally 1 second for each character I type. How can I improve the same?</p>
  
  <p>One approach would be to create the initial array while the view loads for the first time and then make Ajax requests to the array subsequently.</p>
  
  <p><strong>Solved</strong></p>
  
  <p>I put the JSON in session while page load. Subsequent Ajax requests are served from this JSON stored in Session. This is working as of now and Ajax responses are superfast. Will monitor the performance over time in case JSON becomes big in size.</p>