murilo's avatar
Level 10

What wold be faster ?

I need to make a search , to toogle prop favorite ; what search wold be faster , using contains or where () ?

option 1 ) using contains

$tooglePropFavorite = $prop->FavoritedBy->contains($user->id) ? $this->deatatchProperty($prop, $user)
                                                                                          : $this->atatchProperty($prop, $user);


protected function  atatchProperty($prop, $user){
        $prop->FavoritedBy()->attach($user->id);
        return true;

    }

    protected function deatatchProperty($prop, $user){
        $prop->FavoritedBy()->detach($user->id);
        return false;
    }

option 2 ) using where

tooglePropFavorite = count($this->property->Favorite->where('customer_id' , $this->user->id )) ? $this->deatatchProperty()
                                                                                                      : $this->atatchProperty();

does have any laravel method that I can count the speed of the search ?

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

In the context of your question, you're trying to determine which method is faster for toggling a "favorite" property in Laravel: using contains or using where. Let's break down both options:

Option 1: Using contains

$tooglePropFavorite = $prop->FavoritedBy->contains($user->id) 
    ? $this->deatatchProperty($prop, $user)
    : $this->atatchProperty($prop, $user);

protected function atatchProperty($prop, $user) {
    $prop->FavoritedBy()->attach($user->id);
    return true;
}

protected function deatatchProperty($prop, $user) {
    $prop->FavoritedBy()->detach($user->id);
    return false;
}

Option 2: Using where

$tooglePropFavorite = count($this->property->Favorite->where('customer_id', $this->user->id)) 
    ? $this->deatatchProperty($prop, $user)
    : $this->atatchProperty($prop, $user);

Performance Considerations

  1. Eager Loading: If FavoritedBy is a relationship that is eager-loaded, using contains will be faster because it operates on a collection that is already in memory. This avoids additional database queries.

  2. Database Query: Using where will likely result in a database query unless the relationship is already loaded. This can be slower if the relationship is not eager-loaded.

Recommendation

  • If FavoritedBy is already loaded (e.g., through eager loading), using contains is generally faster because it checks the collection in memory.
  • If FavoritedBy is not loaded, using where might be more efficient as it directly queries the database for the specific condition.

Measuring Performance

Laravel does not have a built-in method specifically for measuring the speed of these operations. However, you can use Laravel's built-in logging or a package like Laravel Debugbar to profile your queries and see which approach is faster in your specific use case.

Here's a simple way to measure execution time:

$start = microtime(true);

// Your code here

$end = microtime(true);
$executionTime = $end - $start;
Log::info("Execution time: {$executionTime} seconds");

This will log the execution time of the code block, allowing you to compare the performance of both methods.

Please or to participate in this conversation.