
We’re excited to hear your project.
Let’s collaborate!
“Will WebAssembly replace JavaScript by 20XX?” This is one of those “sensationalizing” news of the moment, right? But still: if we were to run a WebAssembly vs JavaScript performance comparison, which one would be the winner?
And would we have the same winner for different implementations?
We're all looking forward to the future of web development now that WebAssembly has come around to “tempt” us with near-native performance to the browser.
Yet, most of us still write code in JavaScript, despite the predictions of its “imminent extinction”. And there still are use cases where JS outperforms WASM.
So, let's find out:
Just think about it:
We've been having JavaScript as the one and only programming language to be used natively in web browsers and then... WebAssembly stepped in.
“But what is WebAssembly more precisely?” Is it a really an assembly language, like its name suggests? Well, here's a hopefully clear enough definition for you to ponder on:
WASM is a new type of code — with a small-sized fast binary format — for modern browsers. A “compile target”, if you wish.
One that you get to use for compiling any programming language (JS here included).
And NO:
It is not an assembly language, it's not built for a specific machine.
And no, you don't write code in WebAssembly: you use it to compile any given language.
What it does is compile higher level languages and then run those web apps in the browser a lot faster than JavaScript (due to its lightweight, low-level binary format, remember?)
Now that we've seen what WebAssembly is and what it is not, let me briefly outline the key features that set our 2 “contestants” apart:
JavaScript:
WebAssembly:
“Why is WebAssembly faster? How does it work?”
Here's WASM in action:
And it gets loaded, parsed, and executed way faster compared to JavaScript.
Why?
Because its binaries are lighter than the textual JS files and, therefore, faster to decode...
Before I run an “enlightening” WebAssembly vs JavaScript performance comparison, let me highlight the use cases where WASM “shines supreme” as a web performance “booster.
First of all, when you say “common uses cases for WebAssembly”, think about all those performance-critical cases:
WebAssembly's built as a target for writing in-browser software.
In short: think of all those use cases where JavaScript would usually struggle to reach the needed level of performance.
And now, let's get specific:
In short:
Now that we've settled that WebAssembly is usually faster than JS, let's:
“Why?” Because they're smaller than JS's textual files.
By comparison, JavaScript would need to:
… the code before executing it in the browser.
Although it's:
… JavaScript still needs more time to do all the needed work before executing the code.
In other words: there's no garbage pile-up to impact performance.
Any WebAssembly vs JavaScript performance analysis would point out that WASM comes with some significant time-parsing improvements.
Here's why it decodes much faster than JavaScript:
By comparison, JavaScript would need to:
Just think of the JS-heavy web apps striving to parse all that data in due time.
WebAssembly is proven to score 3 times better at load time.
In a WebAssembly vs JavaScript “duel” WASM would always perform better on larger array sizes, powering extremely fast web applications.
The moment they get stored in the cache of the browser, WASM files are easier to load, compared to JS's source code.
Once fully optimized, WebAssembly is slower when executing code in the browser.
And this is partly (some) browsers' “fault”:
On Microsoft edge, for instance, WebAssembly executes terribly slowly.
Well, that's the goal, at least:
To go beyond its common use in web browsers.
To upgrade it from a web-based solution to the go-to option for:
Moreover, one of the “forecasts” is that we'll no longer talk about a “WebAssembly vs JavaScript” rivalry in the future, but about a cohabitation of the 2:
You'll still be able to write your code in JavaScript all while leveraging the speed that WebAsssembly brings to the table: improved frameworks and libraries.
“Will WebAssembly replace JavaScript by 20XX?”
I'm certain that it won't: JS is still a convenient and fast choice for too many tasks.
Yet, we will witness a successful collaboration of the 2.
Photo by Chris Liverani on Unsplash.
We’re excited to hear your project.
Let’s collaborate!