Sergey Tsibel
Sergey Tsibel

Reputation: 1625

Laravel 4 Facades Performance

Is there is any difference in performance using static or non-static methods in Laravel 4 models? The same question for custom classes. Should I use Facades with ServiceProviders to access custom class or just use classic OOP way? For instance which one would be faster to perform 1 million insert operations:

UserModel::insert($user); 

vs

$UserModel = new UserModel();
$UserModel->insert($user);

Upvotes: 1

Views: 467

Answers (2)

Joseph Silber
Joseph Silber

Reputation: 220106

  1. UserModel::insert($user) is not a facade (Laravel Facades are resolved through the IoC container, which does incur some minor performance cost). It just calls __callStatic, and lets Eloquent create the new model instance for you.

  2. Technically, creating your own instance would be faster, since you're doing exactly what Laravel does in __callStatic, so you're saving one function call. But the difference is so minuscule, as to not matter at all.


When I jump, am I closer to the sun than you are?

Upvotes: 2

The Alpha
The Alpha

Reputation: 146239

There is not a big difference for performance in both approaches, when you use Static call to a model, it just calls the __callstatic magic method and from there, Laravel make an instance of the original class and call the method and if you manually make an instance of the original class and call the method manually then the Facade won't be used and that time the Facade uses to resolve the original class will be saved but it's not a very big difference.

So, in my opinion, you may use the common approach that Laravel provides and stay consistent but you are free. The, static call is an easy way to call the methods. Follow the common approach that everybody uses, it's better not to be different when you are working in a project with others.

Upvotes: 0

Related Questions