Home

How, When And Where JS Works

So, how does JS do everything it does? Where is it's best use? To understand how it works, when and where, first, we need to speak of its nature.

Nature of JavaScript

JS is a single threaded and single concurrent programming language which means it can handle one task at a time or, in other words, a piece of code at a time. It’s an interpreted programming language, and like most scripting languages, it uses dynamic typing, where type safety is verified at the runtime. But how JS runtime handles tasks or executes asynchronous codes?

JavaScript is asynchronous by nature, meaning it is designed to ensure the non-blocking code execution. Non blocking code do not prevent the execution of another piece of code. This means that the server works in much the same way, waiting in a loop for a network request, and accepting more incoming requests while the first one is being handled.

Asynchronous programming is great for a faster execution of programs but it is difficult to develop and most of the time you might end up with callback hell situations. So to avoid it you can modularise your code, use generators, promises, event-driven programming or Async.js.

Where And When Does JavaScript Work?

Lightness of JavaScript

JS is considered a lightweight programming language and one of the many reasons is that it does not have any variable types, unlike other languages. It has a very small memory footprint and is easily implemented. This is important especially for porting.

JS becomes more lightweight as more and more libraries and frameworks are released. Vue, Ember, Angular, Riot, Polymer are some of the top JS lightweight libraries.

This interactive language has also a great support for learning, great debugging tools available and much more. It gives an opportunity for a really fast development, which obviously increases productivity. It also has high-scalability, meaning that you don’t spend much on infrastructure as you need less hardware to handle the same amount of load.

So, what to do with JS and what not to do? So, here is the short list of where JS is the most effective, learnt from our own experience.

What You Can Do With JS

#write web applications for browsers

Generally, HTML by itself is not that smart. It doesn’t know maths and is not functionally rich. That’s why here JavaScript is the winner. JavaScript makes a web page intelligent and respondent. Basically, a browser understands only JavaScript․ And as you already guess this is the reason that there are a lot of UI Frameworks based on JavaScript, including famous and popular React, Angular, Vue, Ember, Meteor, etc. Among the top popular web applications written in JS you can find Paypal, Yahoo, NetFlix, Slack, GoDaddy, etc.

#create browser extensions

Google Chrome and Firefox added an ability to create extensions/add-ons using web technologies (HTML, CSS, JS). Extensions allow us to add functionality to browsers without diving deeply into native code. If you've ever built a web page, you should feel home with extensions. JavaScript is great for building extensions because you can modify user browser functionality, add some new texts, functionalities like translate, proxy connection etc. Maybe you know about Adblock Plus, HubSpot, LastPass. They are quite popular browser extensions that are written in JS.

#make desktop apps

Electron opens a new way to develop cross-platform desktop applications using pure JavaScript․Yes, there are more native ways to develop desktop apps, but using electron you will not face any performance issue and your application could run in Macs/Windows/Linux with the same code base. Slack, Lightshot, Mozilla, Greasemonkey are desktop apps developed in JavaScript.

#run it on microcontrollers

Traditionally, it’s been hard to debug hardware. Node.js is being adopted in IoT use cases for rapidly prototyping before products get hardened and mass produced. For many IoT companies and situations, Node.js is tremendously popular - it’s easy to use and rarely breaks. It is fast, has lots of plugins, is scalable and good at I/O, allowing to control IoT from one point, hold effective and secure communications, and more. Engines that allow JavaScript to interact with a microcontroller are a lot, including V8. But V8 uses a lot of memory while a microcontroller can have 200K RAM. That's why there are other engines which are using low memory and providing all APIs to JavaScript. This means you can reuse your existing code to build your app to run on the microcontroller. Here are some IoT libraries that are famous among hardware engineers: Johnny-five.io, Zetta.js, Cylon.js, Nodered, Device.js.

#create native mobile apps

React Native, a JavaScript code library developed by Facebook and Instagram, released on Github in 2013, allows us to achieve both performance and great portability when building a mobile application. It uses the same design as React, letting you compose a rich mobile UI from declarative components, at the same time, creating cross-platform mobile apps which will run on Windows/iOS/Android almost with the same codebase. The number of mobile native apps developed in JavaScript is growing really rapidly. Among popular mobile applications that were built with React Native you can find Facebook, Walmart, Bloomberg, Instagram, Soundcould Pulse and others.

What You Should Not Do With JS

#machine learning

Our team tried to run some machine-learning algorithms in JavaScript and create our neural-network to detect user's face, draw pictures, detect numbers of the machine, etc.

And we have learned, JavaScript is not as good for machine-learning as good it is for the Web. We noticed that computations are taking a lot of time, and in some cases computations weren't correct. Why? Because JavaScript numbers are 32byte floats,  What if we need to user 64byte numbers? There's no way, that's why we moved our ML project to run on Python which is great for ML.

#command line tools

We know you can develop command line tools using Node.js, and that will be okay. But what if you need to develop not small, but big command line tools like git, docker, etc. You'll need multi-threading and fast computations. We tried to build our history-transactions tool which will run in command line, we used Node but when the app grew, we noticed a lot of performance issues in various computers and VMs, even if it wasn't out of memory exception, the program worked very slowly. So we ported our program to Go which provides excellent multi-threading system with its own co-routines mechanism.

Shifting To JS From Other Languages

In some cases, there’s a need to change code base to another language or re-write server-side code to improve performance.

Always there’s something that needs to be optimized. Many big companies are shifting their code base to JavaScript from PHP, Python or even from Java, like Microsoft, Google, Yahoo, Mozilla or Github. Now let's take some languages one by one and see why they do it.

Java To JS

Java is a great language, one of the best in the industry. One of the reasons they shift from Java to JavaScript is the main differences between the syntaxes of these two languages. In Java, you can’t write code outside the class. Some developers love Java just for this and many don’t.  Also, though Java is fast it uses a lot of memory. This is another reason why people are shifting from Java to JS. Paypal, for example, is one of the big companies that migrated from Java to JS.

Ruby On Rails To JS

The world’s biggest professional social network, LinkedIn, too, shifted its server side code entirely to Node.js. They migrated from Ruby on Rails for many reasons. As Kiran Prasad, VP of Product at LinkedIn, stated in one of his interviews the first reason was scale. Node also is really good at talking to other services like API and database. Shifting to JavaScript also made LinkedIn mobile app two to ten times faster than it was before. Shifting from Ruby on Rails also makes the software more lightweight.

Php To JS

If you are a PHP developer and want to change your domain then it is good to go with Node.js. Node.js has low learning curve to get started and has over 250,000 packages on NPM available to assist you during development. It is asynchronous and has non-blocking I/O loop by default, in addition to this it has a very large community supporting it. Whereas, if you want to port your PHP code to Node.js due to scalability issues then here, too, Node.js is a very good option. It can easily handle 10k simultaneous connections due to its asynchronous nature.

Every language has its pros and cons. So if you want to rewrite your entire server-side code, think first. Probably you can break your server to microservices and use what language you want instead of rewriting all codebase from scratch. Think Slow, Act Fast!