Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the JVM cannot be used in place of WebAssembly?

As far as I understood, JavaScript cannot be compiled ahead of time because of it's dynamic nature. So Interpretation and just in time compilation happens at run time, that affects JavaScript performance. So WebAssembly comes into picture. Languages can be compiled ahead of time into intermediate format (WASM). This gives good performance since there is less runtime overhead.

My question is why JVM cannot be used in place of WebAssembly VM. Java compiled into intermediate format (bytecode). This byte code can be given to browser and JVM can execute it. JVM also supports JIT which helps to achieve near native performance. 

So what is the need for new WebAssembly. Why can't JVM be integrated into browser and achieve high performance by leveraging the existing most popular Java language.

like image 451
Jawahar Avatar asked Sep 27 '19 09:09

Jawahar


People also ask

How is Wasm different from JVM?

Personally, my biggest takeaway from the differences between JVM and WebAssembly is that Wasm is platform-independent and provides near native speed for whatever architecture you're running on all with support for many languages, guaranteed security and the fact that you can stream the Wasm binary in the browser.

Can Java be compiled to WebAssembly?

JWebAssembly, from I-Net Software, is a Java bytecode to WebAssembly compiler that takes Java class files as input and generates WebAssembly binary format (. wasm file) or text format (. wat file) as output. The target is to run natively in the browser with WebAssembly.

How is WebAssembly different from Java?

Java, for example, is executed with the JVM (Java Virtual Machine) while . NET languages are executed by the CLR (Common Language Runtime). WebAssembly is a bytecode format that any WebAssembly-capable runtime can execute.

Is WebAssembly a virtual machine?

WebAssembly (abbreviated Wasm) is a binary instruction format for a stack-based virtual machine. Wasm is designed as a portable compilation target for programming languages, enabling deployment on the web for client and server applications.


2 Answers

There are a great many reasons why the JVM was not deemed a suitable runtime in place of WebAssembly ...

  • WebAssembly was designed with delivery-over-HTTP and browser-based in mind. For that reason, it supports streaming compilation - i.e. you can start compiling the code while downloading.
  • WebAssembly was designed to have rapid compilation times (resulting in web pages that load quickly), this is supported by having very simple validation rules compared to Java / JVM languages.
  • WebAssembly was designed with the concept of a 'host' environment, i.e. the browser.
  • WebAssembly was designed to be secure and simple, minimising the overall attack surface.
  • WebAssembly was designed to support a great many languages (C, C++, Rust, ...), whereas the JVM was initially design for a single language, Java.

As a general observation, WebAssembly was designed to support multiple languages on the web. The JVM was designed to support Java on the desktop. It doesn't make either one better than the other in a more general sense.

Finally, the JVM was integrated with the browser (Java Applets), but that didn't work out in the end!

like image 196
ColinE Avatar answered Sep 24 '22 19:09

ColinE


A quote from the High-Level Goals of WebAssembly:

a Minimum Viable Product (MVP) for the standard with roughly the same functionality as asm.js, primarily aimed at C/C++;

So their original goal was running C/C++ program in a web browser, not running Java code.

like image 33
Bumsik Kim Avatar answered Sep 24 '22 19:09

Bumsik Kim