The Pros and Cons of Using JavaScript 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.
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 dataset comprises 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.
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
Learning and Predicting
Linear Regression
[Image: Linear Regression Results]
Precise results:
JavaScript = 7.116 seconds — Python = 0.068 seconds
Random Forest
[Image: Random Forest Results]
Precise results:
Python Training = 14.991 seconds — Python Prediction = 0.799 seconds
Neural Network
[Image: Neural Network Results]
Precise results — Training:
JavaScript = 195.634 seconds — Python = 61.213 seconds
Precise results — Prediction:
JavaScript = 7.366 seconds — Python = 2.030 seconds
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.
FAQs
Q: Can JavaScript be used for machine learning?
A: Yes, but with limitations.
Q: Is Python better than JavaScript for machine learning?
A: Yes, in most cases.
Q: Can JavaScript be used for high-volume machine learning?
A: No, not yet.