Why algolia search take more time than normal laravel search?

Hello community,

I am new to algolia. This is first time I am implementing algolia in laravel project.
I am creating demo of comparison of normal search and algolia serach. I am comparing time taken by both search.

I have uploaded screenshots as well as code.

Please help me out.

Thank you in advance.


This is index method of PostController.

public function index(Request $request)
 {
     $elapsedTime=0;
     $start = microtime(true);
     $returnSearchdata = $request->get('titlesearch');
     if($request->has('titlesearch')){
         if($request->has('searchtype'))
         {
             if($request->get('searchtype') == 'normal') 
             {
                 $searchTerm = $request->get('titlesearch');
                 $posts = Post::with(['user', 'category', 'tags'])
                             ->when($searchTerm, function ($query) use ($searchTerm) {
                                 $query->where('title', 'like', '%' . $searchTerm . '%')
                                     ->orWhere('content', 'like', '%' . $searchTerm . '%');
                                 })
                                 ->paginate(10);
             }
             if($request->get('searchtype') == 'algolia'){
                 $searchData = $request->titlesearch;
                 $posts = Post::search($searchData)->paginate(10);
             }
         }
     }else{
         $posts = Post::with(['user','category','tags'])->paginate(10);
     }
     $end = microtime(true);
     $elapsedTime = ($end - $start) * 1000; //miliseconds
     return view('posts.index', compact('posts','elapsedTime'));
 }

This is post model

type or paste code here```


This is my index blade file.

<div class="container">
    <div class="row justify-content-center">
        {{-- <div class="col-md-8"> --}}
            <div class=""> {{--<div class="card"> --}}
                {{-- <div class="card-header">Posts</div> --}}

                <div class="card-body">
                     <h2 class="text-primary">Posts</h2>
                    <form method="GET" name="searchForm" id="searchForm" action="{{ route('posts.index') }}" >

                        <div class="row mb-5">
                            <div class="col-md-9">
                                <div class="form-group">
                                    <input type="text" name="titlesearch" class="form-control" placeholder="Enter Title For Search" value="{{ old('titlesearch') }}" autofocus>
                                    <input type="hidden" value=""  name="searchtype">
                                </div>
                            </div>
                            <div class="col-md-1">
                                <div class="form-group text-end">
                                    <button class="btn btn-success" id="search">Search</button>
                                </div>
                            </div>
                            <div class="col-md-2 text-end">
                                <div class="form-group">
                                    <button class="btn btn-success" id="algoliaSearch">Algolia Search</button>
                                </div>
                            </div>
                        </div>
                    </form>
                    <p>Request took {{$elapsedTime}} seconds to fetch <span class="text-danger">{{ $posts->count() }}</span> filtered records.</p>
                    <p id="demo"></p>
                    @foreach ($posts as $post)
                        <div class="card mb-3">
                            <div class="card-body">
                                <div class="row" >
                                    <div class="col-md-8 text-start">
                                        <h5 class="card-title">{{ $post->title }}</h5>
                                        <p class="card-text">{{ $post->content }}</p>
                                    </div>
                                    <div class="col-md-4 ">
                                        <h5 class="card-title text-end">Category :  <span class="badge bg-info">{{ $post->category->name }}</span></h5>
                                        <h6 class="card-title text-end">Tags :
                                            <span class="card-text">
                                                @foreach ($post->tags as $tag)
                                                <span class="badge bg-secondary">
                                                    {{ $tag->name }}
                                                    {{-- {{ $loop->last ? '' : ', ' }} --}}
                                                </span>
                                                @endforeach
                                            </span>
                                        </h6>
                                        <p class="card-text text-end">
                                            <small class="text-muted">Published by {{ $post->user->name }} on {{ \Carbon\Carbon::parse($post->publish_date)->format('d-m-Y') }}
                                                </small>
                                        </p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    @endforeach
                    {{$posts->appends(['titlesearch' => request('titlesearch'),'searchtype'=> request('searchtype')])->links() }}
                </div>

            </div>
        {{-- </div> --}}
    </div>
</div>