Sign up ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free.

In dynamic languages, such as JavaScript or Python, the type of a variable is determined at runtime. This is one reason why they are slower than typed languages such as Java.

How is type checking performed? What is the essential reason this process is slow?

share|improve this question

migrated from Dec 15 '11 at 2:22

This question came from our site for professional and enthusiast programmers.

They aren't slower because they are dynamic, they are slower because it's harder to make them faster. JavaScript is actually the most optimized and is quite fast. –  Derek Litz Dec 15 '11 at 4:13

3 Answers 3

up vote 3 down vote accepted

There's confusion in the question.

There's an assumption that type checking is slow, which isn't necessarily the case.

The question also seems to confuse the process of type dispatch with type checking, and they are two different things. One is a process that's done at run time, the other a process at compilation time. I suspect the question is really asking about type dispatch.

It is type dispatch that can introduce overhead at runtime, because the computation spends time with instructions that decide, dynamically, what action to take, based on the types of values it sees at run time. e.g. in a dynamic language, if I apply "+" on two things, I might mean numeric addition, or string concatenation, so I need to spend time looking at what's at hand to decide what to do. There are evaluation strategies that can reduce the cost of dynamic dispatch. (e.g. tracing JITs)

With regards to doing type-checking in JavaScript, see: For a more general overview of how type checkers work, a standard programming language textbook will cover the algorithms. For example,

share|improve this answer
I've also written a small survey about tracing JITs and dynamic languages in –  dyoo Dec 14 '11 at 15:54
Javascript interpreter carries tag bits with each value for type dispatch. Could you elaborate a bit on this? For example, what's the use of tag bits? Does a bit correspond to a type? –  dalibocai Dec 14 '11 at 16:03
The concept of a type is not always connected to representation. We might have a concept of a 'mile' type vs a 'kilometer' type, for example, and it's reasonable to have a language that can statically detect, at compile time, whether computations are improperly applying operations on values that mess up the types. You might imagine that they'd have the same representation, and if the compiler can, at compile time, guarantee that they're never mixed, then there's no reason why the values would need the additional labeling in the representation. –  dyoo Dec 14 '11 at 16:49
Continuing: but often, especially in dynamic languages, you want to represent values of different types. There are several ways to do this discrimination. Type tags are common, but there are other techniques. For example, you might place certain types in blocked-off regions of memory. See "Representing Type Information in Dynamically Typed Languages." for a comprehensive survey of representation techniques. –  dyoo Dec 14 '11 at 16:50

Very basically, in untyped languages, every reference point to an object that contains both the type and the value. For example var a = 3 points to an instance that contains the value 3 and the type int, if you make a = "bla", the reference is updated to an instance that contains the string "bla" and the type string, the old object is discarded, etc...

This is slow because every time an operation (eg a + b) must be made on these basic type, the runtime must first dereference the objects, check that their type are compatible, perform the operation, create a new object.

In contrast, a + b in C++ or Java checks at compile time that the types are valid and compatible, then a and b are stored as immediate values (not references), and the addition is a simple processor operation on these values.

Of course, this is all very theoretical. In practice, a lot of optimization can be done on this process to avoid most of the overhead, and dynamically typed languages can get quite fast.

share|improve this answer
Tricks like polymorphic inline caches can greatly improve performance. David Ungar's (Self) and Eliot Miranda's (Squeak, Visual Works Smalltalk virtual machines) writings are most informative, regarding dynamic language performance. –  Frank Shearar Dec 14 '11 at 15:44

Every value is stored together with its type, which one needs to inspect first. Also conversions say from string to number go via inspection, on the fly.

share|improve this answer
Yep, this is it, it's just a runtime check, nothing fancy. –  anon Dec 15 '11 at 4:17

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.