DEV Community

Discussion on: Python for JavaScript Developers

junebug12851 profile image
June Hanabi

Python is a general purpose language like Javascript, I imagine they do all the same stuff coming from a Javascript developer. Everything you mentioned Javascript also already does such as writing desktop applications to building scripts, system monitoring, etc... Javascript also has a large number of frameworks (I'm referring to well maintained projects and frameworks that are a life and time-saver not some of the over polluted ones on npm) - I think it would also be a great advantage for python users to learn Javascript. Both are general purpose scripting languages and both do about the same thing on same platforms with same resources from the community just different ways of going about it.

Furthermore, and I'm open to correction because I could be wrong, Javascript may have better performance because it's almost always partially or completely compiled down to pure machine code while running and additionally some pretty intense optimizations are applied when compiled, some of them downright ingenious. I've seen some Javascript code outperform C++ like by 100 to 1000 times. I'm aware of Python having compiled code as well but as far as I know it's an explicit action you have to take through the command line to first compile it before running it but I'm not sure if the python interpreter actually compiles as it's running or to the efficiency of modern Javascript interpreters.

Regardless though both are heavily used and both have a lot of practicality in most things given their pretty general modular programming languages with a large community behind them so it'd be beneficial for programmers of one language to learn the other.

ikemkrueger profile image
Ikem Krueger

On the first run, the python interpreter creates „intermediate machine code“. The second run is cached.

Thread Thread
bmarkovic profile image
Bojan Markovic

Python interpreter doesn't create machine code. The code it creates is called p-code (or "intermediate representation", or "bytecode"). Incidentally the "p" does not stand for Python, but for Pascal, because the concept was first used in UCSD Pascal implementation. At that time majority of interpreters were interpreting the source text directly (which is much slower than parsing into AST or P representation).

However that is starkly different to what modern JavaScript runtimes do. JavaScript is converted to an AST intermediate representation in the first run but in subsequent runs it is compiled in much the same way that C, or Go, for example, are compiled. I.e. all the way to native, binary machine code for the CPU it's being executed on.

This design is based on JVM, and it's called JIT (just in time) compilation. Obviously due to it's dynamic typing systems a lot of object/variable handling is still done by the runtime rather than the direct code (but in many cases JIT optimizes code paths if it can infer it's exclusively dealing with, say, strings), which is why even V8 often executes code slower than, say, HotSpot, even if they both are very optimized JIT VMs.