If vs When - Let's See Which One Performs Better - Laravel Performance Tip

If vs When - Let's See Which One Performs Better - Laravel Performance Tip

Laratips

2 года назад

5,566 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

Laratips
Laratips - 01.08.2021 06:00

In the next video, we will be looking at why is this happening by looking into how the when works under the hood.

Ответить
fahath AMM
fahath AMM - 01.02.2022 00:52

Thanks sir

Ответить
Othmane Nemli
Othmane Nemli - 29.09.2021 23:45

I'm sorry, the title might not be correct, because what you show to developers (juniors) will lead them to may not use "when" ever.. "when" should be used mainly to build queries. What you did in every way is not recommended to loop over 100k with if or when both of them are useless;

Anyway Thank you for all your vids !

Ответить
Sohail Khan
Sohail Khan - 19.09.2021 12:04

I'm looking for solution.. how can we search database on the basis of relationship.. like i have a situation.. a field technology and other is version.. both has different tables and the pivot.. i want to search a technology with specific version if exists in relation

Ответить
Remco Smits
Remco Smits - 18.09.2021 13:52

The message that you output is shorter so it takes less time….

Ответить
Dale Ryan Aldover
Dale Ryan Aldover - 18.09.2021 11:38

One thing to always keep in mind is, if a method passes a closure (an inner function) it would take longer time to execute.

Example of closure is the one you just tested:
$user->when(<argument>, function() { <callback> });

Of course there are cases where you need to use a closure, like when doing complicated queries. And remember you always have an option to cache the query.

Ответить
Petyo Ruzhin
Petyo Ruzhin - 17.09.2021 16:37

What is your VSCode setup? (extensions, etc...)

Ответить
Mujahid hamid
Mujahid hamid - 17.09.2021 12:26

Thanks to you great content , i wonder if you can check the performance difference between whereHas and joins and wherein in retrieving Relationships data

Ответить
Criber87
Criber87 - 17.09.2021 10:04

`->when` creates an eloquent builder instance and therefore is much slower, compared to the plain if.
@OP Please check the documentation before comparing pears and apples.

A relevant use case can be an already instantiated query builder and adding builder conditions depending on the current system state. when will be more readable.

Ответить
Ariel Mejia
Ariel Mejia - 17.09.2021 07:47

I understand completely the ide of executing the code 100, 000 times but I am pretty sure that executing a single time both would not have a great difference, of course, there are great use cases for when() and others for if() but generally it would not have that amount of impact in terms of number of executions.

Ответить