Comprehensive JavaScript Knowledge Notes
JS Promise - To the point explanation
| Compiled | Interpreted |
|---|---|
| First, need to compile and then run | Usually go line by line |
| Usually don’t compile if there is an error in the code | Code will run partially if an error is there |
| C++, Java, Rust, Golang, etc. | Java-Script, Python, etc. |
| Static | Dynamic |
|---|---|
| Variables must be declared with a specific data type. | Variables can change their type during program execution |
| It is more performant because the compiler has more information about data types and optimizes accordingly | Provides an easier and quicker development process, as there is no need to explicitly declare types. Makes code more concise |
| C++, Java, Rust | Python, JS, Ruby |
JavaScript is single-threaded, meaning it can only process one task at a time in a single sequence or thread of execution. It can use only one core of computer at a time. It uses the concept of an event loop to manage asynchronous tasks without blocking the program.
In a computer, there are typically 8 or 10 cores, each capable of executing one task at a time. The more cores you have, the better you can multitask. Cores can handle multiple tasks through context switching, allowing you to run more than 10 tasks on your computer even with 10 cores.
JavaScript is single-threaded, meaning it only uses one thread. This can be considered a disadvantage for scalable systems because if you rent an AWS machine with 10 cores and run a Node.js server, it will only utilize one core, leaving the others wasted.
There is a way to utilize all the cores of a machine. By using 'cluster' in Node.js, you can create multiple instances of your application, each running on its own core. When you set up a server using clusters, multiple instances of your server are created. When a request comes in, it is distributed among all instances using a round-robin algorithm.