0

I have a form with autocomplete feature as you type. The feature is slow. An average typing speed is faster than the autocomplete list. You can finish typing before the suggestion pops out. It uses jQuery to and makes an ajax call to a URL which calls a function within a controller that then runs all the list containing the typed string:

The network tab in the chrome developer tools only showed me how long the URL that gets called took and its about 700ms.


public function searchResponse(Request $request){
        $query1 = $request->get('term','');
        $c1=\DB::table('T_EMPLOYEE_SERVICE_AREAS');
        if($request->type=='EmpService')

            {
                $c1->where('EmployeeService','LIKE','%'.$query1.'%')
                            ->groupBy('EmployeeService')
                            ->get();
            }

           $c1=$c1->get();        
        $data=array();
        foreach ($c1 as $service) {
                $data[]=array('EmployeeService'=>$service->EmployeeService);
        }
        if(count($data))
             return $data;
        else
            return ['EmployeeService'=>''];
    }

Aside from maybe optimizing the above query - how would I go about checking where the the most time is spent. I have added a link of the exact same autocomplete script I got from another forum http://www.expertphp.in/article/laravel-5-autocomplete-mutiple-fields-using-jquery-ajax-and-mysql

halfer
  • 19,824
  • 17
  • 99
  • 186
Screwtape007
  • 185
  • 1
  • 2
  • 16
  • small things $query1.'%' is faster because it will use the index rather than full table scan also if you want to make the results array you can do ->get()->toArray(); but i think you would want your results in json for autocomplete ajax – Ahmed Aboud May 16 '19 at 20:17

1 Answers1

1

You should simply annotate your code with some timestamps. For example:

$start = microtime(true);
foreach ($c1 as $service) {
    $data[]=array('EmployeeService'=>$service->EmployeeService);
}
$time_elapsed_secs = microtime(true) - $start;

That will give you an idea for how much time the foreach() loop takes.

Here is a more complete example of profiling: https://stackoverflow.com/a/29022400/7578556

Andy White
  • 438
  • 2
  • 11