This Is Where JavaScript Beats Python For Machine Learning

In my previous article, I compared the performance of Python to JavaScript when training a machine learning model. Though JavaScript should have had a computational advantage, it didn’t stand a chance against Python.

Data processing is Python’s strong suit. In contrast, as illustrated by my study, JavaScript can only handle smaller datasets. Truth be told, Python outperforms JavaScript on many fronts when it comes to machine learning, especially in terms of the availability and maturity of ML libraries.

That said, despite JavaScript’s apparent shortcomings, many new libraries are emerging — while existing ones are gathering an ever-growing developer community. And there’s a good reason for that.

JavaScript is more than capable of running pre-trained models in NodeJS, even a web browser. And this very much aligns with the world today: some five billion people use the Internet (in Poland alone, Internet penetration is estimated at around 76% of the population), while 90% of web pages use JavaScript.

You could even call JavaScript the undisputed king of programming languages. And no data science engineer worth their salt should ignore the chance to reach so many users — or work with the gargantuan dataset that web browsers can provide.

But this begs the question: Shouldn’t we train models using Python, then run them in the browser? My response would be, ‘No… because different use cases benefit from different approaches.’

I’ll explain this point shortly — but first, let’s consider the pros and cons of Python versus JavaScript.

Pros And Cons: Python Vs. JavaScript

Python

There are several benefits of using Pythons to train models. But there are also specific limitations to consider.

Pros

Cons

  • Maturity: there’s almost always a high-quality library available, with more than 275,000 packages available via the Python Package Index.

  • Training Time: it takes much less time to train a Python library

  • Privacy: both the model and the surrounding code are private.

  • Flexibility: you have the option to convert a model into many different formats.

  • Workload: to consume a Python model, you need to deploy and maintain an API (or install a local Python environment with libraries, or create an executable file)

  • Desktop-only: you can’t go mobile using Python.






JavaScript

JavaScript has its advantages. But there are several inevitable shortcomings as well.

Pros

Cons

  • Deployment: it’s easy to deploy, you can even use a static website with your Javascript added.

  • Ubiquitous: you can gather and connect data from multiple sources (e.g., social media)

  • Reach: you can access end users via a pop-up, no need to install anything else. 

  • Mobile: you can port to mobile using React Native.

  • Availability: JavaScript works offline with the right configuration.

  • Lightweight: JavaScript is lightweight.

  • Online-only: the model won’t work offline (unless you configure the web app to work offline, which is rare)

  • Reliability: performance relies on the end user’s device.

  • Public: your code and model are publicly visible 

 





Use Cases: When JavaScript Works Best?

We know the theoretical gains of deploying a machine learning model in a web browser — but let’s make the theory concrete by exploring three real-life examples.

1. UX/UI Quality Testing

A visitor decides if they like a website within 500ms of the site loading, while an Adobe study warns that 38% of users who don’t like a web page will never return to the site. What’s the takeaway? If your website is confusing, unappealing, or in any way unsatisfactory, there’s over a one-in-three chance you’ve lost a potential customer for good.

But you can avoid this. When testing a new layout, you can deploy a model that uses footage from a web-cam to show if a user likes a design… or finds it confusing — and you can try this for yourself below.

2. Gaming

When was the last time you gamed? Were you playing against a computerized opponent? If so, you will have no doubt noticed your opponent had their own ‘will’ — and it’s all thanks to what’s going on in the browser.

Browser-based games have become increasingly complex and visually appealing in recent times. You can even play them offline and still have a capable opponent. Again, that’s all courtesy of the AI that’s running on the end user’s device. 

Since COVID, browser-based gaming has become more popular than ever, with the global gaming market estimated to hit $30bn by the end of 2020. If you want to game and you remember one called Kinect, why not try our very own Tomasz’s version, which he built during a hackathon.

Very neat, huh? And it all happens in the browser.

Read also: The Pros and Cons of Using JavaScript for Machine Learning

3. Communication

The pandemic has forced us to change our habits. Many have moved their office into the home, which has come as a bit of shock. Video meetings are now routine, which itself is presenting somewhat of a dilemma.

Video-based communication has unraveled the privacy of our home: a personal picture on the wall, books cluttering the screen, family wandering by — there are many aspects we might not want coworkers (or clients) to see. 

Thankfully, browser-based machine learning is helping to maintain our privacy with nifty features found in the likes of Zoom and Google Meet. You can simply replace your background, as you can see below.

There’s A Time And Place For JavaScript.

JavaScript has its shortcomings.

But its use in web browsers gives it a considerable role in machine learning, not least because almost every Internet-enabled device connects to a browser.

This degree of availability of artificial intelligence opens up a raft of unique business opportunities, enabling you to collect and use data for machine learning, which could ultimately benefit us all.

That’s why I still believe in the power of JavaScript for machine learning — now, back to a compelling game of chess as I try to defeat AI!

https://dlabs.ai/resources/whitepapers/how-to-implement-ai-in-your-company/

Krzysztof Miśtal

JavaScript Tech Lead focused on everything JS. He is also fascinated with cameras.

Read more on our blog