Comparing Node.js and Python on the Raspberry PI

Picture of my simple led circuit connected to the Pi.

Picture of my simple led circuit connected to the Pi.

Programming on the Raspberry Pi

Python seems to be the more popular language for writing programs on the Raspberry Pi.  However, it is far from the only language: Python, C, C++, Java, and Ruby are some that are automatically supported out of the box.  As it turns out Node.js is also supported.  Perhaps, just based on my preconceptions, I didn’t originally consider JavaScript when interfacing directly with hardware.  The goal of this article is to compare functionally equivalent sample programs written both in Python and JavaScript.


The Goal

I decided to make the scope of the problem created for this article as small as possible.  Given that the goal will be to create simple circuit diagram consisting of a LED and then to write a program that makes the LED flash for a specified period of time.


Simple LED circuit diagram

Simple LED circuit diagram


With the Pi, a circuit that will make a LED flash is relatively simple: Consisting of just a LED and resistor.  I arbitrarily selected pin 7 on the Pi for this build.  I’ve included a diagram of it on this page.



As the goal states, the following two implementations do nothing except toggle an LED at half the specified duration (0.5s) and automatically shutoff after 60 seconds.



Since Python is the more traditional programming language for the Raspberry Pi, let us start with it.  The Rpi.GPIO module I used can be installed via pip.  Before you read the code sample, let me point out some things about the implementation.  Could it have been done simpler given that the problem was just to make the LED flash for a certain amount of time?  Yes, but I wanted to create something of an asynchronous process that could be controlled from outside its execution context.



I used pi-gpio to interface with the GPIO on the Pi in JavaScript.  It can be installed via npm. It makes use of gpio-admin so that your script doesn’t have to run as sudo.  Follow the installation instructions provided here and it should get you setup.  Note that due to differences int the languages, it was not necessary to thread (web workers) the solution.


The Findings – I was surprised

When I started writing the JavaScript side of this article, I was mainly doing it to gain experience working with JavaScript on the Pi.  I did not expect to come out of it vastly preferring it over my python implementation.  I find the Python implementation above to be to wordy and overcomplicated.  It just seems that the amount of code needed to achieve the same results is excessive.  Perhaps this is because of the problem scope and implementation.  The problem posed here was a simple one, where a functional solution is superior to an object-oriented one.  Could the Python code above be rewritten into something a bit more functional? Sure!  Are there problems that an object-oriented Python or JavaScript implementation would be the better solution for?  Definitely.  The lesson to take away: be open to working outside your comfort zone and pick solutions to problems based on their fit for the problem not your comfort level with them.

I’ve created a copy of the code as a Gist that is available here:


  1. Interesting read, thought that your preference for the JavaScript version was interesting as well. Although it is tighter and might perform better the Python version seems much more readable.

  2. Agreed, I’m a fan of the saying “Python is executable pseudocode.” I like it, its very readable and it doesn’t have the quarks that javascript does. Run it with pypy and it will beat out node performance-wise. I think the feel of the javascript appealed for the event-driven nature of the code sample.

  3. Despite of the amount of codes needed for achieving the same work, did you compare the runtime of these two languages on RaspberryPi? Somehow I heard a saying, that Node.js runtime could consume most of the 512MB RAM and cause the Pi to crash after running for a long time, yet this seemed not happen to Python.

    “The new Garbage Collector (GC) algorithm in node.js v0.10.x behaves differently than v0.8.x – in that it doesn’t enforce a clean until it is near a memory limit larger than the 512MB in the Pi – this can cause the Pi to crash if left running for a long time”, excerpted from nodered website.

  4. I didn’t compare their footprint at all. I’ve not left anything of substance running for a prolonged amount if time in Node on the PI. I’d not seen the post on node-red before you mentioned it since I compiled from source. I did see where they mention (below the quote you used) the use of the “max-old-space-size” argument that would fix the issue.

  5. If you can install nodejs, then you can upgrade python to 3.5. In that case, you can do an elegant functional implementation:

    import asyncio

    import RPi.GPIO as GPIO

    async def flash_led(pin, duration):
    GPIO.output(pin, 0)
    await asyncio.sleep(duration / 2)
    GPIO.output(pin, 1)
    await asyncio.sleep(duration / 2)

    def stop(coro): = False

    loop = asyncio.get_event_loop() = True
    coro = flash_led(7, 0.5)
    loop.call_later(5, stop, coro)

    Which is not only shorter than the JS version, but easier to read (no callback hell) and way easier to debug or refactor.

Leave a Reply

Your email address will not be published. Required fields are marked *