Disclaimer

Please be aware much of this is my opinion. Whether or not it is a good one is up to you.

What is Python?

Stated in this Wikipedia page :

"Python is an interpreted, high-level, general-purpose programming language. Created by Guido van Rossum and first released in 1991, Python has a design philosophy that emphasizes code readability, notably using significant whitespace. It provides constructs that enable clear programming on both small and large scales." -Wikipedia

Who uses Python?

Home applications, like Home Assistant, and Enterprise companies, like Netflix and Paypal, use(d) Python. Python has been around for more than 2 decades, you can be sure many applications have used it.

If everybody's doin' it, Why not?

It comes down to these main points :

  • Performance
  • Learning Curve
  • Scalability
  • Real-Time Data Management
  • Developer Fluidity
  • Error Handling

Performance

Here is a performance test found from 3 years ago. Only Python and Node.js will be shown below. For the full review please follow the link.

The following samples of code were run on an Intel Core i3, 8GB of RAM, OS on an SSD by Dong Nguyen. This is Node.js.

var sum = 0
for (var i = 0; i < 100000000; i++) {
    sum +=i
}
console.log(sum);

and this is for Python.

sum = 0
for i in range(100000000):
    sum += i
print(sum)

Dong's results are as follows :

  • Python : 7 to 8 seconds
  • Node.js : 0.02 to 0.12 seconds

Now it is mentioned that this test is from 3 years ago. How are they doing now? Sadly I don't have a build with the same specs so I tested on an Intel Atom N455 1.66GHz, 2GB RAM, OS on an HDD.

  • Python : 49 minutes, Process crashed before completion
  • Node.js : 1.429 to 1.697 seconds

Either my computer is too slow or Python has gotten worse. Let's keep in mind that this machine was the first development machine for Shinobi (A Node.js application). The machine may be gutless but it isn't as useless as Python makes it out to be.

Now, some of you who reviewed the full comparison probably saw the comment by tom hasc stating the following about the test

Using python's range while using for() in other languages isn't fair. In python either use simple while loop or xrange

I decided to see what he meant. The difference was shocking but not shocking enough to change my mind.

Here is a reworked test for Python following Tom's recommendation.

count = 0
while (count < 100000000):
   count = count + 1
  • Python : 47.033 to 47.144 seconds

But if Python is going to get itself a while loop then Node.js should get one too :) So here is a reworked test for Node.js. Yes, I like them curly brackets.

i = 0
while (i < 100000000) {
  i++;
}
  • Node.js : 1.160 to 1.848 seconds

Python does a lot better with a while loop but still can't hold a candlestick to Node.js.

Learning Curve

In previous years some would say Python is easier to understand simply because it has been around longer. It has had more time to have documentation written for it. Now that Node.js has had more time to mature; the documentation has as well.

With the growing number of open source Node.js applications it won't be long before people are just as comfortable with Node.js as they are with Python... if it hasn't happened already.

In the performance test sample code you can see that Node.js takes a bit more code to do the iteration but that is an older style of writing and can now be written like this :

sum = 0
for (var i = 0; i < 100000000; i++)
    sum +=i
console.log(sum)

Looks a lot like Python doesn't it? You can forgo the addition of curly brackets and the statement var to write in a similar way.

Although there is a serious drawback to writing like this. In Python you must make sure the indent spacing is perfect. If it is off by a little on any line you will have yourself some serious errors. When writing in Node.js without curly brackets; the spacing of indents are less important and the possibility of an error from indents are slim.

Statements as simple as switch are either not available, used in an inefficient or "ugly" way. Here is an example.

Python "switch" function

def numbers_to_strings(argument):
    switcher = {
        0: "zero",
        1: "one",
        2: "two",
    }
    return switcher.get(argument, "nothing")

Node.js switch statement

function(argument){
    switch(argument) {
        case 0:
            return "zero";
        case 1:
            return "one";
        case 2:
            return "two";
        default:
            return "nothing";
    };
};

Node.js is slightly longer but by it being a native statement to the language it makes more sense to a developer on first glance. You could know nothing about the switch statement but instantly understand how it works just by seeing this example.

Scalability

This is best explained by DA-14 so I will paste an excerpt and link to their article.

In short : Node.js is non-blocking by nature and if the app is written correctly it will scale smoothly. Python doesn't come with async features out of the box but tools are easily available to expedite the process in building a scalable application.

Another important aspect which needs to be taken into account in planning backend development is scalability. Scalability is the ability of an application to serve the increasing number of requests with no compromise in performance. This ability is essential in content-heavy applications as well as in those serving multiple new users, both through desktop and mobile interfaces.

The programming language also has its influence on the application scalability. Node.js creates a single-thread asynchronous architecture with I/O operations completed outside the thread and, therefore, not blocking it. This feature guarantees smooth Node.js scalability within simple web apps, however, development of complex applications with a lot of concurrent processes requires in-depth knowledge, attention, and careful engineering research.

Python, in its turn, does not support asynchronous programming by default, but it supports coroutines with which asynchronous processing can be easily achieved. So, even if its architecture may seem not as scalable as that of Nodejs, Python has the tools with which necessary scalability can be reached.

https://da-14.com/blog/python-vs-nodejs-which-better-your-project

The way I feel about it is considering the performance factor I will always need a more powerful machine for Python driven applications. Even if I am capable of driving 100 machines together in Python it would be much more beneficial to do those same tasks with 5 machines running Node.js.

Real-Time Data Management

Node.js is great with holding variables in memory for fast receipt and review of data sets. When it comes to communication machine-to-machine, Node.js takes the cake. You can make use of libraries like Socket.IO and PeerJS with amazing performance.

There are Socket.IO libraries for Python but they're CPU intensive compared to the officially maintained Node.js counterpart. There are other Websocket libraries available but Socket.IO, I believe, is the best wrapper. It makes bi-directional communication from Client-to-Server or Server-to-Server a no-brainer and Node.js makes it quick and easy to make use of it.

Developer Fluidity

I thought this up as I wrote this article so here is what it means to me :

The level of ease and speed in which a developer can write and test their code.

Node.js and Python are both wonderful in the sense that they compile when you run them. Unlike C++ where you may have to wait a decent chunk of time to just get usable libraries. This makes testing code a lot easier.

The advantage that Node.js truly has over Python is when it comes to writing applications that have client interfaces as well. Node.js is built on JavaScript, so are websites. With Node.js we can easily make web panels without having to switch our "internal language processor" to another language. You can just keep it on "JavaScript".

Even something as basic as starting a web server is streamlined in Node.js. Here is an example with Express :

const express = require('express')
const app = express()
const port = 3000

app.get('/', function(req, res){
    res.send('Hello World!')
})

app.listen(port)

Now here is an example with Python and Flask :

from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    return 'Hello world'

if __name__ == '__main__':
    app.run(debug=True, host='0.0.0.0')

Both are using libraries to streamline building a web application but as you can see Node.js wins in readability yet again.

Error Handling

DA-14 mentions that Python is easier to make sense of errors, which I can somewhat agree with. Node.js is just a little more chatty. The one thing that you must keep in mind is that Node.js prints top-bottom and Python prints bottom-top.

What does that even mean? Let me show you with some stack traces.

Python failing to load "cv55"

Traceback (most recent call last):
  File "failedPyTest.py", line 1, in <module>
    import cv55
ModuleNotFoundError: No module named 'cv55'

Node.js failing to load "express"

internal/modules/cjs/loader.js:583
    throw err;
    ^

Error: Cannot find module 'express'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:581:15)
    at Function.Module._load (internal/modules/cjs/loader.js:507:25)
    at Module.require (internal/modules/cjs/loader.js:637:17)
    at require (internal/modules/cjs/helpers.js:22:18)
    at Object.<anonymous> (C:\Users\Matrix\Documents\test\expressFailTest.js:1:77)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)

As you can see Node.js prints the most relevant data at the top, rather than at the bottom. Keeping this in mind makes reading the stack traces in both languages a non-issue.

Now reading an error is one thing, finding a solution is another. Many of my hurdles with Python took more effort than my hurdles with Node.js. I feel it is because many issues faced with writing JavaScript we're already faced by people making websites and web apps. An inherent knowledge of the back-end already existed with a large number of web developers. It gave Node.js a pre-existing community base where one normally wouldn't exist for something new.

What I wouldn't use Node.js for

The major thing I wouldn't use Node.js for (at least not today) is Machine Learning. I will always advocate the use of C++ in this circumstance. C++ can be highly optimized for the machine it runs on. The hurdles are worth it though because C++ can run 200 times faster than Node.js.

Why not just use C++ for everything? Well C++ is a pain to write in and test for, so it's best to use it only when necessary.

Luckily with Node.js we can make use of C++ code. We can call C++ functions from within the JavaScript code base. Python can do this too but its base execution speed makes it almost meaningless.

To emphasize how much better combining Node.js with C++ really is, you can take a look at these demonstrations.

Here is a test done with Node.js using Python and a traffic stream to do YoloV3 Object Detection.

Here is a test done with C++ and a looped video (simulated live stream) doing YoloV3 Object Detection as well.

The first thing you'll notice is how much faster C++ actually is. I've only set this detection speed to 5 fps but i have successfully achieved 10 fps and expect that it can go higher depending on the GPU used. Python couldn't get past 2 fps before the script started crashing.

You're probably thinking : but I've seen Python do better than this.

Frankly, me too. Although I have never seen Python do well on a network live stream. I have only seen Python do well with pre-recorded videos or directly connected cameras, like USB cameras.

Conclusion

Node.js brings high performance and smooth usability under one language. It is consistently maintained and has many more years to come in the hands of developers; big and small.

You can run Python code from Node.js as well so you can make use of existing Python code while harnessing the power of Node.js. Although Python isn't looking too good as a language or as a community catalyst. Even the original author of Python is stepping back without appointing a successor.

It is also now seen more often that companies are migrating their high demanding systems to Node.js. That includes companies like Netflix, PayPal and IBM. Let's just say Node.js keeps astronauts safe.

I'll probably be labelled a "Node.js fanboy" and I'm okay with that because Node.js is worth being a fan of.