Saturday, July 6, 2019

Laravel : Laravel Eloquent vs query builder - Why use eloquent to decrease performance?

Yes, In some case you are right. When we've more data and almost in every site, data is not small really. Then it is better to use DB Query than the Eloquent Query.
In a performance issue of Eloquent VS DB I've heard that,
To insert 1000 rows for a simple table Eloquent takes 1.2 seconds and in that case DB facades take only 800 mili seconds(ms).
So Why then Eloquent ? Is't any necessary of that ?
Answer is - Eloquent is also necessary. Cause-
To create a better relationship and get the results in view with so much simple syntax, when there needs to join.
Eloquent is also for who have not much knowledge of SQL query.
An MVC framework follow the rules of Code Readability, Code Maintainability and which Eloquent is, you know that. A code comparison below. Obviously, Eloquent is better to read.
// In Eloquent
$student = App\Student::find($id);

// In DB facade
$student = DB::table('student')->where('id', $id)->first();
Most important part is if we want to change other database, then raw query will be a lot much headache to us and in that case Laravel Eloquent will solve all the problems with one hand. It can handle different types Database.
So when we use Eloquent and When DB facades:
  1. When we'll work on a simple and small records site with simple CRUD and there records are not fact, then use Eloquent there.
  2. When we'll work on a lot's of records, it is better to use DB Query than Eloquent.
So, finally it is cleared that - when we'll use Database Query and When we'll use Eloquent Query.
Edit - Real Life Example
  1. I'm making an University website. Which may contain maximum 5,000 teachers and 10,000 students and some notices and files. Then it is better to do this with simple Laravel Eloquent which is very much standard and readable.
  2. Now I'm making a site like Stackoverflow. Which may contains more than 1,000,0000 (1 crore) posts and many more things. I will must choose the conventional DB facades there. It is more faster for searching the posts from so much records.
You can check your query performance using Laravel Debugbar (A popular package to check Eloquent/Database query performance/ execution times)
Now it's your choice. What you want to make...

JS : Event Bubbling and Propagation

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>
    <div id="m">
        <div id="d">
            <div id="p">
                <div id="s"></div>
            </div>
        </div>
    </div>
</body>
</html>

<script>
// Event Bubbling and Propagation
//  element.addEventListener( type, func, useCapture);

let m = document.getElementById('m');
let d = document.getElementById('d');
let p = document.getElementById('p');
let s = document.getElementById('s');
let log = console.log;

let highlight = (ev)=>{
    //add CSS class "gold" to the clicked element
    ev.stopPropagation();
    let target = ev.currentTarget;
    target.className = 'gold';
    reset(target);
}

let reset = (_element)=>{
    setTimeout(()=>{
        _element.className = '';
    }, 2000);
}

d.addEventListener('click', (ev)=>{
    ev.stopImmediatePropagation();
    log('Hi I\'m a DIV');
});

[m,d,p,s].forEach((element)=>{
    element.addEventListener('click', highlight);
})
</script>