lsvidal / javascript-vs-native-addon-prime-numbers

Speed up your Node.js App with Native Addons. What’s faster, C++ or JavaScript?

Home Page:https://medium.com/@fhinkel/speed-up-your-node-js-app-with-native-addons-5e76a06f4a40

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Speed up your Node.js App with Native Addons. What’s faster, C++ or JavaScript?

This example compares JavaScript to C++ in Node.js for computing prime numbers.

JavaScript is a ridiculously fast scripting language thanks to modern JavaScript engines. But how fast is JavaScript compared to C++? This repository contains an example that computes prime numbers in Node both with JavaScript and with a native C++ addon.

Usage

git clone git@github.com:fhinkel/javascript-vs-native-addon-prime-numbers.git
npm install
npm start

Experiment with this example yourself. Just remember to run npm install to recompile the addon if you change any C++ code.

Results

If your Node app’s sole purpose is to compute prime numbers and you don’t want to use a fast lookup table, please rely on this benchmark 100%. For anything else, this benchmark is probably useless.

The JavaScript and the C++ implementation use the same algorithm. Calling into the addon and running the computations in C++ is faster than staying in JavaScript except for small prime numbers, i.e., the first 25 prime numbers.

Computing the first 1,000,000 prime numbers without warm up

Computing the first 1,000,000 prime numbers with Node 9.2.

Computing prime numbers without adaptive optimizations

Computing prime numbers without adaptive optimizations.

License

MIT

About

Speed up your Node.js App with Native Addons. What’s faster, C++ or JavaScript?

https://medium.com/@fhinkel/speed-up-your-node-js-app-with-native-addons-5e76a06f4a40

License:MIT License


Languages

Language:JavaScript 57.0%Language:C++ 32.0%Language:Python 10.9%