## DEV Community

Posted on

Ryan

Your friend is coming over to play.

Eager Loading: You open the toybox and pull all the toys out.

When should we use them?

Eager Loading: You open the toybox and pull out the toys you know you are going to play with. Your friend arrives and you start playing right away.

Lazy Loading: You aren't sure what your friend wants to play, so you wait for her to arrive.

When should we not use them?

Eager Loading: You open the toybox and pull out every toy just in case you will play with it. Your friend rings the doorbell. You can't answer because you're still pulling out toys. Eventually you answer the door after she's been waiting a while and let her in. When she leaves you have unused toys all over the floor.

Lazy Loading: Your friend's favorite toy is your cowboy figure. But you don't like it, so it's buried at the bottom of the box. Your friend arrives and wants to play with the cowboy. She has to wait for you while you dig through the toy box to find it.

rhymes • Edited

Someone gives you a box of candies.

Lazy loading: you're hungry, you know where the box is, you open it and eat all the candies, you close the box

Eager loading: you open the box, you put all the candies in your mouth, you close the box and when you're actually hungry you start eating them

candies = resource
box = connection
mouth = memory
eating = resource usage

Hopefully you only get a mouthful of candies, because if they are too many you can't fit them in your mouth so eager loading won't work and you'll end up with an issue :-D

Philip Hallstrom

I can't seem to get passed the "put all candies in mouth" and then NOT eating them immediately :)

K

You get math homework on Friday.

Eager loading has the benefit, that you can simply point to the solutions when asked and don't need to do anything anymore.

Lazy loading has the benefit, that you don't have to do anything if nobody wants your solutions.

Anwar

I will give a shot and try to explain it like you are five... with 1 year of exp in HTML and 1 year in SQL, which is the regular profile of our internships at my company :joke:.

When you browse a website with an image gallery using an infinite scroll, and when you scroll down too much, next images begin to load, and you scroll again, and new images appear, etc... That is lazy loaded: image will not load untill you asked to see them.

Imagine a web page that list hot posts on dev.to, with for each post the title, the excerpt, and an "author" button. If you press the author button and you see the page displaying a loader, that was not eager loaded, but lazy loaded! Eager loading would have been the dev.to post page to instantly show the author name, location, and email on click on this button without loading, because it has loaded them ahead. We could rename this in "loading ahead of time", so when you do eager loading you are assuming things (maybe unefficient: if the use will click on the author button an average of 5% of the time, but your page is loading 100% of author's post anyway, you waste CPU and bandwidth nothing 95% of the time).

Everton Agner • Edited

You just moved to a new house. You packed all of your belongings inside boxes. The truck just left all of them inside your new home, and you're about to start your new life in there.