WebAssembly: Beginning of a New Era

Original author: Eric Elliott
  • Transfer
The web has a great future.
Yesterday, Brendan Ike “blew up” the web development community : the web will receive a new low-level binary compiled format that will work much better than JavaScript.
Google, Microsoft, Mozilla, as well as several independent experts are working on a new project at the W3C WebAssembly Community Group , and what they are working on is not a small thing.

WebAssembly is:

  • Improving JavaScript: Implement all critical things on wasm (short for WebAssembly - translator's note) and import it as a standard JavaScript module.
  • New programming language: WebAssembly defines an abstract syntax tree (like JavaScript) in binary format. You can write code and clean it from errors in text format. WebAssembly is easy to read.
  • Improvement for browsers: Browsers will understand the binary format, which means that developers will be able to compile binaries, which can compress much more than JavaScript text files used today. The smaller the file, the faster the download. Depending on the possibilities of optimizing compilation time, the code on WebAssembly can be transmitted and run faster than on JavaScript!
  • Purpose for compilation: The ability to other languages ​​to receive first-class binary support through the entire stack of the web platform.

What does this mean for JavaScript?

Before answering this question, let's rewind time back. Back in the days before React, Angular, Backbone, and jQuery.

Here we are.
A network is a set of hypertext documents displayed using message delivery systems, which, however, are not yet interconnected. The first web server was launched on a NeXT computer (see below) at CERN ...

image

In the yard 1991. I have not had time to turn gray. I'm trying to hack a 10,000th text-based quest game (I haven't counted them all).

I chose a kind of programming language for this purpose. At that time, I was already fed up with both BASIC and Pascal. I wanted to use C, but I couldn’t: I was still saving to the boxed version of Borland Turbo C ++ (they literally come in packaged boxes with instructions and an installation disk). Then I did not have even the simplest Turbo Assembler.

I wrote in assembly language and “compiled” into an executable program using the command line in DOS and “debug”. If that sounds crazy, believe me, it was. I bet that even those of you who were lucky enough to work with DOS do not know that you can use “debug” to debug assembler and disassemble (reverse engineering) existing code.

Sounds cool? Not. I hated that. I could not wait for the moment when I can sit down for the brand new Borland Turbo C ++ and program humanly. In general, they gave it to me. Hurrah!

I liked the Borland Turbo C ++ because the Borland Turbo Assembler was preinstalled on it. What?! Why program in assembly language when you have a wonderful high-level, object-oriented C ++ tool?

Sometimes you want to work with bare iron, or close to it, without melting the brain. By the way, did I mention that I wrote a lot of machine code before using C ++?
I've gone mad.
Achieving real results is not so easy when you write code in assembly language. Then why do we need WebAssembly?

We need WebAssembly because with all the flexibility of Javascript, it’s still hard to imagine many things that we would like to see in it, and the features of the language that we need to add there will only complicate the language, which already confuses users .

WebAssembly allows us to work with simple, low-level blocks of code that can be used to program anything.

How is it different from JavaScript? The keyword is “low level”. It defines primitives, including a number of types and operations on these types, literals for them, control-flow, calls, heaps, etc.

These are very simple primitives. Nothing complicated. There is no complex object system (prototype or some other). There is no built-in, automatic “garbage collection” that follows you and periodically stops you when it needs to collect garbage.

In fact, it will be useful not to think of WebAssembly as a language in general. It looks more like a car, a bit like a Turing machine ...

What is WebAssembly?

WebAssembly defines an abstract syntax tree (AST), which is stored in binary format . Binary is great, as it allows you to create smaller applications . Surely you are wondering how you can debug binary code.

Fortunately, a debugger is being actively developed that will work in browsers, and the abstract syntax tree will be presented in a (relatively) easy to read text format . I would like to show examples, but so far there are not very many of them. Perhaps the WebAssembly code will still be more difficult to read than the similar code written by hand in JavaScript, but at least it will be as easy to read as ASM.js. Maybe even easier. We will see.

What will WebAssembly be used for?

Among other things, it can be used for simple work with threads and SIMD (single instruction, multiple data) - in other words, with one stream of commands and several data streams. You can queue many data blocks, and then write one command to work with them at the same time.

This means that parallel processing of streaming video will be processed by the processor. If you keep your finger on the pulse, you heard about this decision in JS, but I always found it inconvenient to solve low-level things using JavaScript.

It is in such cases that you should probably forget about the object system, “garbage cleaners” and dynamic query processing. Just queue data streams and process them as quickly and efficiently as possible.

What about apps?

Currently, applications such as Ableton Live (composing music) and Adobe Premiere Pro (creating video) are not well suited for porting to the Web. I note that this is possible, but still difficult. There are still many problems to solve. For example, you need to decide how best to synchronize data for real-time applications.

In addition, it will be possible to develop applications that will transfer large amounts of data through a network of processing functions. For example, such as the application of a guitar pedal . When people think about such things, they usually don’t think about JavaScript. Many people probably think this is sheer madness to even try.

But, in any case, JavaScript is a really great language to build most of the application you can dream of.
WebAssembly seamlessly fills in the gaps that JavaScript has.
There are gaps in the functionality of JavaScript, and this is far from a secret. Even his most loyal fans will hardly argue with the statement that sometimes the language tries to “swallow” too much, losing flexibility and efficiency. Just yesterday, I thought these gaps could be eliminated simply by adding more functionality to JavaScript itself. Brendan Ike suggested the same path at the Fluent conference. I applauded.

However, all this time we missed one point: everyone wants to program in a high-level language, but at the same time, and be able to "lower" to the assembly language when it is necessary to increase speed.
WebAssembly can increase the speed of JavaScript at times!
I’m sure that today there will be dozens, if not hundreds, of articles about WebAssembly throughout the Web. I am more than sure that most of them will be devoted not to what I wrote earlier, but to a completely different feature of this programming language. By the way, I am also delighted with her.

WebAssembly allows you to use more languages ​​in web development

Of course, we don’t really need WebAssembly to use other programming languages ​​in web development. For example, we already have great AAA game engines that work great with JavaScript compilation.
If you hear that JavaScript is slow ... It is not.
WebAssembly adds things that most JS developers do not want to see in JavaScript. The functionality itself is needed, but in JavaScript it definitely does not have a place. Moreover, we can get all these functions using compilation from other programming languages.

In fact, WebAssembly provides us with an alternative compiler - created specifically for this purpose.

Now, it will be much easier for us to port code that is highly dependent on, for example, shared memory chains. I am sure that writing a compiler for WebAssembly will be easier than writing a compiler for JavaScript, and all because the first guarantees the best transfer of language functions to a given abstract syntax tree.

The fact that all old programming languages ​​can now be used without problems on the Web is good, but this is not the main thing. The fact is that:
WebAssembly is an excellent reason for developers to start work on new programming languages.
The network has a great future. So it’s better to get out of the shadows and get down to business before it's too late.

Add-ons + FAQ

Q: What is wasm?
A: Acronym for W eb As se m bly.

Q: Why not use the JVM?
A: There have been attempts more than once to add JVMs to browsers using plugins. Unfortunately, nothing good came of this. JavaScript has a built-in virtual machine, so adding one more leads to the appearance of a second set of connection APIs to give the virtual machine access to the DOM, networks, sensors, input devices, etc. You will have to sacrifice something for this. For example, how will processes in a virtual machine distribute available resources among themselves? Answering this question is harder than it sounds.

At first, WebAssembly will work as ASM.js polyfile, that is, it will be able to use the JavaScript virtual machine. The design of the language has developed on the basis of this, and that is why WebAssembly will have a more smooth integration with browsers than alternative virtual machines can offer.

Q: Does the appearance of WebAssembly mean that in the future there will be many new programming languages? Would this lead to fragmentation?
JavaScript is completely safe. Its ecosystem will flourish for many more years. WebAssembly is more about performance, diversity, and moving forward rather than fragmentation.
A: JavaScript has always had very serious competition on the server side, as well as in programming for embedded systems such as small computers and robots. Despite the presence of a large number of fairly good alternatives with developed ecosystems and professional development teams, Node continues to rapidly increase its presence on the servers of startups and commercial enterprises.

In addition, JavaScript has good support in the face of a galaxy of developers, as well as a powerful ecosystem. I like to add popularity graphs of various modules to articles. You look at these curves and are amazed.
image
Pay attention to the green curve. This is npm, a standard JavaScript repository that comes bundled with Node.

JavaScript is increasingly used in game development, in programming robots and IoT devices. Although there is considerable competition in these areas from C, C ++, and Java, this does not affect the position of JavaScript as the main web programming language. All developers have a choice, and they use JavaScript for a reason, but because they like it.

JavaScript will survive. “There are only two kinds of programming languages: those that people constantly complain about and those that no one uses.” Björn Straustrup

Useful materials

W3C WebAssembly Community Group
Mailing List
IRC: irc: //irc.w3.org: 6667 / # webassembly
GitHub
Who’s involved?

about the author

Eric Elliott is the author of the books Programming JavaScript Applications and Learn JavaScript Universal Development with Node, ES6, & React . He participated in software development for Adobe Systems, Zumba Fitness, The Wall Street Journal, ESPN, BBC, as well as for artists such as Usher, Frank Ocean, Metallica and many others. Eric spends most of his time in Silicon Valley with the most beautiful woman in the world.

Worked on the translation: greebn9k (Sergey Gribnyak), seninrom (Roman Senin), silmarilion (Andrey Khakharev)
Singree

Also popular now: