Nick
Nick Bull Blog

Follow

Nick Bull Blog

Follow
Explain What Single-Threaded JavaScript Means Like You Are 5 Years Old

Explain What Single-Threaded JavaScript Means Like You Are 5 Years Old

Super. Special. Kid. JavaScript.

Nick's photo
Nick
·Jun 5, 2021·

3 min read

You are 5. A couple of years ago you could barely walk and talk. And now you're going to learn the hardest concept in JavaScript – threads. Sounds cool? Let’s start.

Single-Threaded Javascript. Our Super. Special. Kid.

We have a kid named JavaScript. He is different from other children. He’s special.

javascript-vs-java.png

No, he can’t fly.

While other kids are multi-threaded, he is single-threaded. This isn’t a bad thing. In fact, it’s his choice. But JavaScript’s “single-threadedness” makes him completely different than other kids.

When normal kids want to make a hot chocolate and binge-watch YouTube, they can do it at the same time:

multiple-thread.png

But when our special kid, JavaScript, wants to do the same thing, he has to do one thing at a time:

single-thread.png

The difference is that our kid waits until some task is done and, only after that, is he able to start the next one. Other kids perform tasks simultaneously and don’t wait until the prior task is completed.

Just like single-threaded languages are different from multi-threaded ones. Single-threaded languages can perform only one task at a time, while multi-threaded languages can perform them in parallel (all at the same time).

But why can't single-thread languages, special kids like JavaScript, be normal and perform tasks in parallel?

It's all about how the threads work.

Threads. Slides. Dogs.

In programming, threads are essentially single processes that a program can use to perform tasks. You can think of this like a water slide where performing a task is the process of going down the slide.

thread-in-programming.png

Yes, it's a dog. It represents a task.

This is how your computer works: each thread can execute only one task at a time. Everything else is blocked until an operation is completed. When one dog goes down the water slide, the other dogs have to wait. Only one dog in the water slide at a time. Only one task at a time in a thread.

how-threads-work-in-programming.png

Our special kid JavaScript is a single-threaded language. He has only one thread. That’s why he can perform only one task at a time. This makes him different from other kids.

Thread. Aquapark. Our child has grown up.

Imagine that our special kid grew up and decided to open a water park for dogs. This isn’t a normal water park. He applied his “single-threaded philosophy of life" to it.

sing thread vs multiple thread.png

Because JavaScript is a single-threaded aquapark that only has one water slide, dogs can't go and have fun on the other slides. They have to stand in line and wait for each dog in front of them to finish riding.

how single thread javascript works.png

Looking at this, I can tell that our special kid JavaScript is going to be a total business fiasco. Can you imagine if one dog takes an hour to go down the slide? Other dogs are waiting. Owners are angry. Dog Guards can't go home. This water park business is a disaster.

problem with single thread language.png

Waiting in line can be boring and frustrating. Why can’t or kid JavaScript do it all at once like the other kids?

Unfortunately, he can't.

He is a special kid and we must accept his single-threadedness. No matter how hard he tries to imitate multithreaded behavior with setTimeout(..., 0) and web workers, he will remain single-threaded.

He will remain our special kid.

javascript is our favorite kid.png

PS: no dogs were harmed in the process of writing this article.

In the end...

If you enjoy this article you need to know that every Monday, I send a letter to 4,000+ Web Developers with 3 hand-picked articles from the tech world, 2 web development guides, and 1 best Tweet of the week

“Your newsletter is f**king awesome, really Nick. It gives me a great start to the week with a lot of insightful knowledge.”

Join smart developers who get short and full of knowledge letters for free.

 
Share this