Dynamic Typed Language v.s. Static Typed Language
Tags: programming
Published: 2024-05-26 12:54:09

The programming itself depends on one thing which is values.

We use values by storing them in the memory, accessing it, and editing it. To access these values we need to put a label on it so whenever we need to use that value we can just basically call it by using the label.

```

int number = 23

```

For example in this case we store the value which is 23 in the memory with a label which is a number. int is just saying to program create a new label.

While we are storing values in the CPU's (the microchip) memory we need to know the type of the value so we can store the value in the correct place. the types can be whole numbers, even numbers, texts, or big objects which has other values stored inside.

So now we want to store some data in the memory but how we will know where to store this? We need to know the type of the value.

To know the type of data we have two options:

- the label itself can store the type of data (static)

- the value can store the type of data (dynamic)

#### Static Programming Languages 

In static programming languages when we are creating a label we need to give it a type as well for example in C++ which is a static language. When you want to store some data.

```

int label1 = 3

string label2 = "hello world"

```

While we are creating label1 we put int keyword upfront which represents a type. it says label1 can only store integers(whole numbers) in it.

In static languages the labels have type values in them because of that when the compiler converts the code to assembly it only checks the label to get type info to know where to store the values.

#### Dynamic Programming Languages

In dynamic programming languages when we are creating a label we don't need to specify the type info the value itself will store the type info for example in Javascript which is a dynamic language. When we want to store some data.

```

var label1 = 3

var label2 = "hello world"

```

For creating a new variable we always used var keyword because var keyword doesn't store type info it is just responsible for creating a label in this case, label1 and label2 don't have a type.

However, the compiler needs the type value to store data in the correct places. for that every time before storing data it will check the value get type info from it and then convert it to the assembly language.

**In the Programming world labels are called variables**

---

### How about speed?
Since we need to check the value every single time in the compiling, the compiler needs to work more compared to static languages.
Because of that, we can say static languages are faster to compile. compared to dynamic languages right?

**Dynamic typing languages are pretty fast but how?**

as an example, I will explain the working mechanism of The [V8 Javascript Engine](https://dev.to/ikbalarslan/javascript-runtime-37i8) 
when the engine starts the execution it will use two things to make it faster:
-JIT(Just in Time Compilation)
-using two compilers instead of one

**JIT**
with using [JIT](https://en.wikipedia.org/wiki/Just-in-time_compilation) the engine immediately will convert the javascript code to machine-understandable code. on the way line by line.

**Two Compiler**
engine uses one normal compiler and one optimizing compiler. Optimizing the compiler will save the type data in the memory for most used functions. so if we call the same function again it won't waste time checking types every time(if the types are the same)

I have explained more details about Javascript engines in [this article](https://dev.to/ikbalarslan/javascript-runtime-37i8) 

---
here are some resources to dive deeper into this topic:
- [this](https://stackoverflow.com/questions/761426/why-are-dynamically-typed-languages-slow) StackOverflow discussion is pretty informative.
- Scala’s author has a good [StackOverflow answer](https://stackoverflow.com/questions/3490383/java-compile-speed-vs-scala-compile-speed/3612212#3612212) on why the compiler is so slow.
- [benhoyt's blog post](https://benhoyt.com/writings/language-speed/)
- [this](https://www.youtube.com/watch?v=p-iiEDtpy6I) presentation from JSconf


---

In this article, I have explained the differences between dynamic and static languages. If you have any questions feel free to leave a comment. I will be happy to answer them.