Performance Comparison: JavaScript vs. Python for Machine Learning
In my previous article, I discussed the pros and cons of using JavaScript for machine learning. I delved into whether it performs as well as Python-based solutions on ML tasks. And now, I’ve put the programming language to the test.
I used several models to measure Javascript’s performance in machine learning, benchmarking the actual results against Python-based solutions.
For context: the task we used for the tests was ‘fraudulent financial transactions detection.’
The Dataset
I chose to use synthetic datasets generated by the PaySim mobile money as they include 6,362,620 records of financial transactions — the datasets comprise eleven columns, and below is a snippet of the data.
The dataset includes:
- 6,354,407 legitimate transactions; and,
- 8,213 fraudulent transactions.
This translates into a 0.1% fraud scale. It’s worth mentioning that fraud only occurs for TRANSFER and CASH_OUT transactions — below, you can find the exact number of transactions per transaction type.
We carried out data analysis and following this, deemed, isFlaggedFraud , nameOrig, and nameDest columns as irrelevant to the result — below, you can find a correlation heatmap between the relevant columns.
Benchmark Environment And Method
The following gives details about the environment and methods used to benchmark the data.
Environment
We performed all tests on machines with the following specifications:
- CPU: Intel Core i7–4770HQ, clocked 2.2 GHz
- RAM: 16GB
- GPU: None
- OS: macOS Catalina (10.15.2)
We used the following software environments:
- Node 12.16.1
- Python 3.7.6
We used the following libraries:
- Python: Pandas, NumPy, scikit-learn, Keras
- JavaScript: Zebras, machinelearn.js, fscore, Tensorflow.js, ModelScript
We carried out measurements for the JavaScript code by calculating the time difference between the Date.now() value at the beginning of a function and the end. We used a broadly similar approach for Python with one exception: we used the time function from the time package.
We measure the execution for the following sections in both programs:
- Data read from the file
- Data preprocessing
- Split into test and train sets
- Learning
- Prediction
We chose just three models for the test:
- Linear Regression
- Random Forest Classifier
- Neural Network.
We measured each metric ten times (on datasets that included all records and reduced to 1m records), and the average results are shown below under the heading, ‘Results.’
Before we look at the results, one aspect worth noting when I reproduced the Python code in JavaScript was the libraries’ immaturity. There have been significant improvements over the years here. Nonetheless, I had to spend a lot of time searching through the libraries to get the same functionality I had with the Python version.
In Python, it’s a breeze. It’s a well-recognized language for machine learning. Therefore, the level of community engagement with the development of libraries is many times higher.
The Results
There are no two ways about it, so let’s cut straight to the chase.
Python wiped the floor with JavaScript — you can see this in the graphs and the full results below.
The Full Dataset
Data read from file
Data preprocessing
Split into test and train sets
Learning and predicting
Linear Regression
Random forest
No, we didn’t make a mistake. We only got Python results as the truth is… JavaScript never made it.
The process didn’t finish working in the given timeframe.
Neural network
Data preprocessing
Split into test and train sets
Learning and predicting
Linear Regression
Random forest
Same here as well: we only got Python results — JavaScript once again didn’t finish the process in the given timeframe.
Neural network
What Does It All Mean?
Sadly, I didn’t manage to test high-volume machine learning this time around. Still, the learnings from the tests I ran are stark. JavaScript couldn’t get close to Python’s tasks — across the board.
JavaScript’s computational performance is still much better than Python’s.
However, the maturity of the libraries — which often have underlying modules written in C — means that operations on large datasets can offer so much more than sheer computational power.
But there is still a place for JavaScript in machine learning. If you leverage ready-to-use models, you can cut the learning time and use resources just to make predictions. While if you already know how to code in JavaScript, it’s fine to use it as a basis to explore machine learning concepts.
Then, when performance becomes important, you can switch to Python.
Originally published at https://dlabs.ai on May 25, 2020.