JavaScript: from alert() to Machine Learning

JavaScript: from alert() to Machine Learning

 

JavaScript has meandered through the dungeons of misconception, misunderstanding and discrimination, trapped between the rock of "toy language" and the hard place of "language of the web".  JavaScript is on the rise, this is no mystery. Many are betting on JS being the language of the future, and calling it the most popular language in the world. I don't trust popularity indexes (well, not 100%) and as far as I'm concerned, I'm happy with JS being the language of the "present". *

Note that I am not primarily a JavaScript programmer, I like strongly typed languages better, but there is something compellingly beautiful about ES2015 (aka ES6) and functional programming in JavaScript so I will be using it for the foreseeable future.

* I for one would like the language of the future to be a future language, one that doesn't yet exist.

Dawn of the Golden Era

In my (and probably many others before me) humble opinion, JS began its real rise with the advent of Node.js (AJAX gave it a first shake-up and jQuery made it easy to work with in the browser). Node.js too has had its fair dose of misconceptions associated with it, for example, remember this genius?

Once we all got our heads round this "event-driven, non-blocking" model, we all saw the potential of this new technology. Except the guy above maybe (don't want to wrong him, maybe he's come around). Not to mention the power of streams!

The popularization of JS and the evolution of the ES spec into ES2015 have contributed to the growth of the once-piddly-little-language-for-rollover-buttons, and massively improved its performance, stability, maturity, number of tools/toolchains/frameworks/stacks available. These changes in the scene did not send JS's weaknesses away: weak typing and implicit conversions, plus several quirks, all hilariously illustrated by the legendary Gary Bernhardt's "WAT" talk:

but they (the aforementioned changes) absolutely helped overcoming them (weaknesses and quirks).

The Ultima Thule of Type Safety

Currently, you cannot prevent JS from operating implicit conversions, but you can use tools to ensure they are not happening in your code, as they are mostly not desirable. (Naturally if you like to turn numbers into strings willy-nilly I'm happy for you).

Ultimately, even if a language has flaws (or  features you don't particularly like), if you protect yourself and your code from these flaws then you should be fine. In theory at least. Also let us be clear on something: weak typing is not a bad thing, it's a language feature. I don't like it, but that's me.

Utilities like tcomb, for example, will check the types used in your code, so you can keep inconsistencies at bay. Another fantastic tool for this purpose is Facebook's Flow (or you can just use TypeScript, I'm just not a fan of transpilers).

The combination of the evolution of the ECMAScript specification, the proliferation of tools and a growing / passionate/ fanatic community (which by the way, is probably the friendliest community of developers) contributed to make JS ready for prime time, even in fields traditionally associated with strongly typed languages such as Machine Learning.

One Lang to Rule Them All

The most remarkable consequence of Node.js is in my opinion the existence of full-stack engineers * with one language. Granted, most "full-stack" engineers didn't get to be that by only knowing JavaScript, but it greatly simplifies the life of a company to only have to worry about educating their developers in only 1 language, and it further simplifies their recruitment criteria. To think that you can write a MEAN application with just JavaScript is quite amazing.

* full-stack engineer is a buzzword which has a feeling of propaganda about it: like, I'm a full stack engineer, you should genuflect in my presence.

But ... is it all good?

No. There are many things that can be improved, one of them being the overcrowded npm panorama. People publish modules and don't always keep up the commitment of maintaining these, and if you happen to depend on one of these un-maintained/zombie modules and something goes wrong...  you're in the wind. Sure,  you can fork/fix/republish with another name, but that's not ideal.

Also, there's a tendency to re-write the same thing all over again. I know there's more than way to skin a cat,  but at this stage there's more frameworks than developers out there. The poor cat is being skinned with laser beams and plasma cannons.

Having said that, the biggest problem Node.js had lied in the controversial way it was "managed" by Joyent, but the io.js/node.js reconciliation seems to promise a better future for everybody.

With political problems being resolved and ES2015 released, JS is ready for prime time.

Things we did not think JavaScript would ever do

Once JS started evolving, the days before the appearance of front-end frameworks became numbered, so in a way frameworks like React or Angular are a natural evolution. But what did we NOT expect? Here's a short list of things that I - at least - did not expect.

Server Side Applications

JavaScript on the server was  definitely not one of the things we imagined possible. And when it became possible, we didn't think it would do it well. And when it did it well, we thought it would never compare for performance and scalability to more traditional languages/ frameworks. Now we kind of ran out of reasons to think JavaScript is not suitable for the  server. Except  again, maybe the guy in the Node.js video above ("JS is SlooooOOOOOOwwWW").

Desktop Apps

To keep it short, NW.js has made this a reality. Kudos.

Data Analysis

JS can be used for Data Analysis or Data Science (again, this also sounds very buzzwordy to me... so up until yesterday we were pulling some statistics from a dataset but today we're data scientists. I always picture a "data scientist" as someone who's looking at a single bit of data, say a "1", under the microscope).

There's a really neat library by Nathan Epstein called datakit which shows off how JavaScript is perfectly capable of Data Analysis.

Databases

Databases written in JS? I've seen it all now. There are a number of incredibly good products out there, such as the very popular and mature PouchDB (nearly 5k on Github) or LocalForage, Dexie or - just to blow my own trumpet a bit - LokiJS.

Computer Vision

That's right! JavaScript can be used for computer vision tasks, take hog-descriptor for example. I bet you never thought you'd see the day in which JavaScript was going to be used to compute Histogram of Oriented Gradients. I bet not even Dalal and Triggs thought that back in 2005.

Not to mention jsfeat which covers pretty much all the most common CV operations including face detection with HAAR cascades (look at this demo). Honestly - the JS Oscar goes to Eugene Zatepyakin.

Machine Learning with JavaScript

I'm not saying Machine Learning is the ultimate goal of a programmer. It's just another branch of computing. But JS is now capable of performing Machine Learning operations without spitting the proverbial "NaN errors"!

So where do you start from if you want to do some Machine Learning with JavaScript? First of all, familiarize yourself with some of the more popular models and algorithms, such as Support Vector Machines (SVM), Artificial Neural Networks (ANN), Naive Bayesian classifier and k-Nearest Neighbors  (k-NN). Note that these are just some of the models, there are quite a large number of them.

Secondly, find libraries that implement those models. Here are some:

  1. brain by Heather Arthur is an ANN library with an outstanding 6k stars on Github.
  2. svmjs and node-svm are implementations of SVM in JavaScript

These libraries are no less than amazing, and enable the JavaScript coder to jump on the bandwagon of Machine Learning and Artificial Intelligence.

Thirdly,  get yourself some data and start classifying.

The web is full of incredible libraries to perform classification with ML, less so of libraries that turn your data into ML-friendly data structures, so I wrote a couple of modules for the purpose:

  1. jotun turns JS objects into vectors
  2. mimir builds a vector for a piece of text using the bag-of-words model

Conclusion

After years of constant progress and evolution,  JavaScript is ready for bad boy stuff, including Machine Learning. We will go through some of the more popular libraries in detail in future posts.

Antonio De Luca

Software Architect, Developer and Enthusiast

7y

What do you think about DN2A - Digital Neural Networks Architecture? You can find it here: https://github.com/dn2a/dn2a-javascript

Like
Reply
Mark Bridges

SVP, Chief Technology Officer at CableLabs

8y

Nice work. Appreciate all the library recommendations.

Like
Reply
Mark Lofvendahl

Business Leader - Agnostic Technology Consultant - Problem Solver - Husband - Father - Avid Golfer

8y

great article, well written

Like
Reply
Jorge Eduardo Ardila 🇨🇴 🇺🇦 🇮🇱

BackEnd REST API Cloud Infrastructure Builder :: | :: Nodejs | Python | Postgres | AWS

8y

Hey Joe, very good review. A nice read.

Like
Reply
Siva Prasad Mamidi

Co-Founder and CTO at Qwipo

8y

Nice article

To view or add a comment, sign in

Insights from the community

Others also viewed

Explore topics