DEV Community

Warren Wong
Warren Wong

Posted on • Originally published at warrenwong.org on

NodeJS vs. Python 3 Performance

Originally published at warrenwong.org.

While trying to become more Pythonic in my coding, I have been going over some of my Project Euler toy problems. It's mostly fun and cathartic, but I did notice something fairly interesting.

On problem 7 of Project Euler, the solution seems fairly simple to implement. My implementation for both is rather similar (no real obvious way to make it utilize some of the newer features of JavaScript or Python 3 that I can see). My assumptions was that it would take about the same amount of time to run.

NodeJS implementation:

const isPrime = n => {
  for (let i = 2; i < n; i++) {
    if (n % i === 0) {
      return false;
    }
  }
  return true;
};

const getPrime = n => {
  let count = 0;
  let num = 2;
  while (n > count) {
    if (isPrime(num)) {
      count += 1;
    }
    num += 1;
  }
  return num - 1;
};
Enter fullscreen mode Exit fullscreen mode

Python implementation:

def is_prime(n):
  for i in range(2, n):
    if (n % i == 0):
      return False
  return True

def get_prime(n):
  count = 0
  num = 2
  while n > count:
    if is_prime(num):
      count += 1
    num += 1
  return num - 1
Enter fullscreen mode Exit fullscreen mode

I mean, it's fairly simple and straight-forward. The time complexity of both should be exactly the same, but I couldn't believe the difference. I ran it several times with the Unix time utility and I consistently got sub 2 seconds for the NodeJS implementation and over 25 seconds for Python 3.




nodejs vs python 3

It's actually incredibly alarming for me to see the difference since I would think the performance would be fairly similar. Since I was curious, seems like other more "formal" benchmarks also confirm this.

Well color me surprised.

Oldest comments (4)

Collapse
 
tarialfaro profile image
Tari R. Alfaro • Edited

Some languages have better performance in certain areas. NodeJS in particular has beaten many languages calculating numbers.

NodeJS is simply one of if not the best scripting languages AFAIK for calculating numbers really fast.

Collapse
 
tarialfaro profile image
Tari R. Alfaro

But I have to admit, the Python implementation is just pleasing to read while I am straining my eyes with the NodeJS one.

Collapse
 
wrrnwng profile image
Warren Wong

Yea, I agree, especially on this specific example.

Collapse
 
logicmason profile image
Mark • Edited

I also get similar results on my system.

Just for fun I copied your algorithm into Rust to compare!

fn main() {
    let num = 10001;
    println!("Prime number {} is {}", num, get_prime(num));
}

fn is_prime(n: u32) -> bool {
    match n {
        0...1 => false,
        _ => !(2..n).any(|d| n % d == 0),
    }
}

fn get_prime(n: u16) -> u32 {
    let mut primes_found = 0;
    let mut i: u32 = 2;
    while n > primes_found {
        if is_prime(i) {
            primes_found += 1;
        }
        i += 1;
    }
    return i - 1;
}

Results

Language Version Time
JS 10.7.3 1.64s
Python 3.7.1 26.24s
Rust 1.35.0 0.78s