Thursday, September 06, 2012

JavaScript is a real programming language

The world is divided into "real" programming languages like C and Java, and then "scripting" languages like Ruby and PERL. Real programming languages are about 100 times faster than scripting languages, with features designed for "large scale" programming projects.

In the past, JavaScript was just another scripting language. As the name suggests, it's a lot like Java, but designed for small-scale scripts rather than large scale programming. (By "a lot" I mean simply that it shares the C-family syntax, and could equally be called "C-Script", "C++Script", "C#Script", and so on. It's actually a retro-name: it was originally named LiveScript, but when Java came out, they renamed it to leverage the name recognition.)

But JavaScript has grown up. Today, JavaScript can be used to write robust, large-scale, server-side applications with performance on par with "real" programming languages.

This can be shown in the following benchmark graph comparing the performance of several popular languages side-by-side. as you can see, JavaScript is consistently faster than "Lua", which has long been known as the fastest scripting language, and is often as fast as the C#-Mono language. It's still slower than Java and C, but not by a lot. This graph is from

Critics will point out that JavaScript is missing many features like "object-oriented" programming. That's not actually true. It's just that the programmer has to build up objects and classes manually rather than having a clean syntax that does it for them.

This leads to "language on language" features, as people strive to standardize the messy way that JavaScript manually builds things. For example, the "require.js" is becoming the standard way for doing modularity (similar to "include" or "import" statements from other languages). This is part of the "CommonJS" spec, and used in popular projects like "NodeJS". Frankly, this "language on language" cruft is no worse than in other languages (for example, like the STL cruft in C++).

The driving force behind JavaScript's maturity is Google's "V8" JavaScript engine. When Google released their "Chrome" web browser to compete against Internet Explorer and Firefox, they released their JavaScript interpreter as a separate component. This allowed JavaScript to be used on the command-line and for services just like any other scripting language.

In addition, V8 focused on speed. It introduced the "JIT" (just in time compilation to native code). Because of V8, the first versions of Chome had a massive competitive advantage over competing browsers in running JavaScript code. The other vendors have caught up and occasionally surpass Chrome in benchmarks today. It's this constant competition that is making JavaScript faster and faster, approaching that of a real language.

In some cases, JavaScript may be even faster than a real language. The above benchmarks are simple algorithms that don't test JavaScripts strengths. The DOM in browsers is a messy data structure. Optimizing messy data structures is therefore a key feature to JavaScript. The end result is that if you are writing an application with similarly messy data structures, it may run faster on a JavaScript engine than in a "real" programming language.

The V8 engine has spawned other projects, most famously the "NodeJS" project. NodeJS is an asynchronous web-server for building web-apps in JavaScript. It's asynchronous event-driven nature means that it's much more scalable than Apache. NodeJS has spawned an entire ecosystem of APIs and addons (managed with "npm", a NodeJS package manager).

Completely separate from browsers and servers, there is also the "ActionScript" dialect of JavaScript, part of Adobe AIR, which has become a popular alternative to Java for writing cross-platform applications.

JavaScript is still missing some things, like an integrated IDE with a source-level debugger, multicore support, light-weight fibers/coroutines, and some other features. But on the other hand, these often don't work well for "real" languages, either, and they are improving rapidly in JavaScript. For example, a popular package for NodeJS includes a source-level debugger -- run through a web-browser, naturally.

That JavaScript is becoming a "real" language means one more thing: it would be an excellent teaching tool. Colleges struggle with find the best language for teaching computer science concepts. Colleges often choose LISP, C, or Java. I suggest JavaScript as the better choice. It has enough "functional" and "object oriented" features to teach these concepts. But the biggest reason is that all students have to learn it anyway, since it's part of the browser (and what computer science geek doesn't do "View Source" on web pages?).

So in conclusion, in the competition among "real" programming languages to find the best one for your next project, consider JavaScript as an alternative.


Anonymous said...

There is support for threads in most JavaScript engines these days via WebWorkers.

Anonymous said...

The language benchmarks game famously removed a bunch of alternative language implementations. The Lua you see there is not LuaJIT ( ) whereas as you pointed out v8 is JIT'd.