Modal Title
Frontend Development / Software Development / Typescript / WebAssembly

JavaScript or WebAssembly: Which Is More Energy Efficient and Faster?

A study compares JavaScript and Wasm on runtime and energy efficiency. In real-world applications, WebAssembly shines.
Jan 30th, 2023 9:51am by
Featued image for: JavaScript or WebAssembly: Which Is More Energy Efficient and Faster?

Which runs faster and is more energy-efficient: JavaScript or WebAssembly? The University of Minho in Portugal researched this very question and came to the conclusion that while JavaScript can be more energy efficient and faster than Wasm when it comes to in-lab micro-benchmarks, in real applications Wasm outshines JavaScript on speed and energy efficiency — sometimes by as much as 30% on average.

And that’s with Wasm still in its early days.

“Wasm is still in its infancy and only time could tell us how it will evolve,” study author João De Macedo, a software engineer, told The New Stack via email. “In our wildest predictions, we see Wasm completely pushing out native apps from operating systems and crowning the web browser as the operating system of the twenty-first century.”

Micro-Benchmarks vs. Real-World Applications

The research, published in 2022, looked at real-world benchmarks, as well as micro-benchmarks.

“A micro-benchmark is a program that tracks and measures the performance of a single well-defined task such as elapsed time, rate of operation, bandwidth, etc.,” De Macedo explained. “Micro-benchmarking is one of the principal ways to measure the performance of a software system, thus, Wasm is no exception.

Since one of the main goals of Wasm is to improve the performance of Web applications, it’s “particularly relevant to compare the run-time and energy performance of Wasm and JS,” he added.

When it came to the micro-benchmarks, in some cases JavaScript could be more energy-efficient and faster than Wasm. While Wasm was more energy efficient and outperformed JavaScript on Google Chrome and Microsoft Edge, JavaScript did have better performance results than Wasm on Mozilla Firefox, with a “significant difference most of the time.”

That said, ultimately Wasm still ruled overall in real-world applications.

“Preliminary results show that WebAssembly, while still in its infancy, is starting to already outperform JavaScript, with much more room to grow,” the report stated. “A statistical analysis indicates that WebAssembly produces significant performance differences compared to JavaScript.”

How the JavaScript vs. Wasm Study Worked

The study looked at how well Wasm and JavaScript performed in real-world applications with the Wasmboy benchmark, which is a Gameboy/Gameboy Color Emulator, written in Typescript to benchmark Wasm. Wasmboy is written in JavaScript/TypeScript and it was created with the main goal of comparing the runtime performance between Wasm produced by the AssemblyScript compiler and the ES6 latest version of JavaScript, as produced by the TypeScript compiler.

“This game console includes six different open source games that can be executed by the console,” the report stated. “We updated the WasmBoy source code in order to specify the browser where the games have to be executed.”

So with six games running across three browsers — Chrome, Edge and Firefox — with two languages, the team had a total of 36 unique samples.

They also used the PSPDFKit benchmark, which uses software that supports viewing, annotating, and filling in forms in PDF documents on any platform. The report noted that the open source benchmark was created to assess the possibility of porting the software to the Wasm ecosystem and comparing Wasm to JavaScript implementations. The team made changes to the application’s source code to execute several inputs in the two languages considered (was and asm.js). To execute the benchmark with realistic inputs, the team considered five different pdf documents, including one book divided into three parts, one scientific paper, and a slide presentation of 20 slides.

“Similar to the Wasmboy benchmark, we developed makefiles to automate the execution in the different browsers,” resulting in five samples in each language across three browsers for 30 unique program executions, the report stated.

It also looked at various micro-benchmarks; these programs were originally written in C and compiled into both Wasm and JavaScript using the Emscripten compiler. Other languages that can used to compile to Wasm are C/C++, Rust, Go, Python and AssemblyScript, a form of TypeScript.

Details of the micro-benchmarks and other aspects of the study can be read in “WebAssembly versus JavaScript: Energy and Runtime Performance,” by João De Macedo, Rui Abreu, Rui Pereira, and João Saraiva, which is available with a subscription on the Institute of Electrical and Electronics Engineers.org or for free in De Macedo’s master thesis.

However, the long and short of it is that this approach allowed the team to look at, for instance, how scale and input size were handled differently by JS and Wasm. A previous study had used this approach, but only looked at performance in virtual machines. De Macedo’s study wanted to look at real-world applications, so the team developed a framework to measure performance within a browser-based environment.

Wasm as a JavaScript Replacement?

I asked De Macedo if Wasm will eventually replace JavaScript.

“Maybe never, because JS makes more sense to web pages that don’t need to be ultra-performance,” he responded. “Currently, Wasm serves as a supplement to JS rather than a replacement. However, if Wasm were further developed, it might take the place of JS in some applications because of its faster loading times and more effective resource usage.”

Long-term, De Macedo predicted Wasm will be disruptive.

“Wasm will not only revolutionize the web,” he said. “The promise of WebAssembly has the potential to upend multiple areas of the technology market — including the cloud — which recently has increasingly shifted to a container model that doesn’t always serve every organization’s needs.”

Group Created with Sketch.
TNS owner Insight Partners is an investor in: PSPDFKit, The New Stack, Real.
THE NEW STACK UPDATE A newsletter digest of the week’s most important stories & analyses.