JavaScript: Fundamental Answers

Fundamental questions loomed at the beginning of JavaScript Week.

Some answers:

Is JavaScript the new bytecode?

Abstractions like Scala, Groovy, and Clojure were built on top of the Java Virtual Machine’s finely tuned bytecode processing power.

Virtual machine innovation is now happening atop V8.

V8-powered NodeJS apps compile directly to machine code at a speed that rivals the JVM’s compilation from bytecode to machine code.

TypeScript, React, and other conveniences are built on JavaScript.

Red herring: WebAssembly and asm.js promise a bright future of a fast, minimal instruction set of JavaScript. But that would be at a lower level than JavaScript itself, so asm.js is the answer to the question “what is JavaScript’s bytecode?”

What does Node.js have to do with Docker and microservices?

Not much. This question was an example of my vulnerability to hype vortices.

What are the tradeoffs between frameworks like Meteor.js, Express.js, and React.js?

React.js is a view layer, not a framework. But it is so compelling and has more resources behind it than perhaps any other JavaScript technology. Studying React today is like studying iOS in 2008*.

React’s Flux architecture describes a workflow that allows developers to optimally leverage React’s front-end. The most Flux-compatible backend is Meteor, another well-funded JavaScript project.

Meteor is also extraordinarily fun and easy to work with. Remember how much fun you had when you used Rails for the first time? It’s like that, but better.

A tradeoff is that, as Mattias said, “Meteor is a bit full-stacky.” Adoption is hampered by the fact that one does not simply integrate Meteor into a preexisting microservice-based, RESTful stack. To build a simple, friendly RESTful JSON service in Meteor is to miss the point of Meteor.

But–there are plenty of other options for a friendly RESTful JSON service:

  • Express.js is the flagship framework for building full-fledged isomorphic JavaScript applications.
  • LoopBack is an end-to-end API framework maintained by the same people as Express.js.
  • restify is a RESTful API framework with built-in DTrace support, which simplifies debugging.
  • Sails.js, Hapi.js, and many others are also available.

If a company has an established strategy for building and deploying microservices in Java or C#, how hard is it to add NodeJS apps to the ecosystem?

Another question rooted deep in the buzzword nexus of the Hype Vortex.

To the consumer, a RESTful API built on NodeJS is no different from one built on the hegemonic service layer of Java or C#. The only difficulty in getting adoption at a Big Company might be weakly founded claims about the unreliable nature of JavaScript.

How do Node apps compare to Java apps in terms of size, speed, time-to-market, garbage collection, and maintenance?

These benchmarks are hard to measure.

What is the relationship between Node.js and V8?

"Node.js uses the Google V8 JavaScript engine to execute code, and a large percentage of the basic modules are written in JavaScript."

"V8 compiles JavaScript to native machine code before executing it, instead of more traditional techniques such as interpreting bytecode or compiling the whole program to machine code and executing it from a filesystem." 


Going further requires looking into V8.**

What is the Node.js single-threaded event loop?


Any time something happens in a NodeJS application, an event is created.

New events are loaded onto the event queue. These events are processed serially. The event loop takes events from the head of the event queue one at a time and hands them off to the file system, network, or another process.

As those events are processed, they create more events. Those new events are placed at the back of the event queue, and the cycle continues.

Is a single-threaded event-based model preferable to a multithreaded model?

Yes. Humans should figure out more ways to behave serially. Multitasking is a myth. Single-threaded event programming is more comprehensible for programmers.

It’s not a stretch to extend this philosophy to how we think about programming models*.

But also no. A Node microservice may be processing each individual event serially, but different services are doing different things at once. In this sense, a Node programmer’s overall system is multithreaded.

Node’s single-threaded event loop is only a part of an overall application that is doing multiple things at once.

*opinion is the author’s own

**Anyone who has performed a comparative analysis between V8, SpiderMonkey, Rhino, and Nashorn, please leave a comment. I’d like to know more about how these differ.