DEV Community

Cover image for Dynamic Typed Language v.s. Static Typed Language
ikbal arslan
ikbal arslan

Posted on • Edited on

Dynamic Typed Language v.s. Static Typed Language

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

Enter fullscreen mode Exit fullscreen mode

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"

Enter fullscreen mode Exit fullscreen mode

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"

Enter fullscreen mode Exit fullscreen mode

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
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 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


here are some resources to dive deeper into this topic:


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.

Top comments (3)

Collapse
 
efpage profile image
Eckehard

I think the correct wording ist "static typed language".

In any case, any language needs to know the type of the data it stores, as this cannot be derived from the byte values in memory. But it can derive the type from the values it gets, so on input it can assume that 23 will be an integer, but 23.1 is a float. This way of automatic type detection is called type inference.

Type inference is just a way programming languages like Javascript work, but this does not affect compilation or execution speed directly. If you think that your claims are correct, it would be good to mention some sources, so readers can get more information on this topic.

Collapse
 
ikbalarslan profile image
ikbal arslan

Thank you for your recommendations,
I have updated the article depending on your recommendations.
Let me know if the new update is informative enough

Collapse
 
efpage profile image
Eckehard

Much better now!

I´m just not sure you are on the right track with compilation speed.

The compilation speed is often depending more on the number of tools included in the pipeline. A traditional C++-compiler performs numerous steps to produce executable code:

  • *Preprocessing *-> transform the source code on a textual level
  • *Compilation *-> generate some machine independent code, recompile all changed libraries
  • *Linking *-> combine all used code modules and build the executable.

Each step can contain multiple substeps like explained here, but most time is often used to check and reload external libraries. This can take some seconds, but also minutes for large codebases.

Javascript on the other side uses a JIT, which precisely means this: It takes virtually no time until code is executed. Indexing and optimization ís often done on the fly, so a loop is optimized after the first iteration. This is highly economic as code is not optimized that is not used. In fact, Javascript compiles much much faster than any traditional compiler. I´m not sure how fast code execution is in a direct comparison, but the people that developed the current engines did an increadible job. I suppose, JS is not much slower than any of the "traditinal" languages, at least if you compare with some older compilers.

On the other hand, the JS compiler is not important in the workflow execution time. If people use Typescript, Babel, Rollup and others, this will much more slow down the process. So, I suppose, there is no general rule on this.

Some comments may only be visible to logged-in visitors. Sign in to view all comments.