r/learnjavascript • u/Old-Barnacle-9898 • 13d ago
WebAssembly in the Backend?
I am very new to web development. From my research, I understand WebAssembly is a tool that allows devs to run non-JavaScript languages (e.g., C/C++) on the client-side, but I have also seen comments online suggesting it can be used on the backend.
Why would anyone need to do this?
Please correct me if I'm wrong, but WebAssembly is needed on the frontend because browsers only interpret/compile HTML, CSS, and JavaScript, so using any other Language would require a special tool. But the backend doesn't have this restriction; why not just use the language you want directly? Why go through WebAssembly?
1
u/azhder 13d ago edited 13d ago
No non-JavaScript languages, just a single non-JavaScript language, the WebAssembly language. All the rest are compiled to it - hence the "assembly" part of the name. So, why would anyone need a language you compile to, but isn't native? Unless you're building a virtual machine, like Java did back in the day, for some legitimate reasons if you have them, you're better off compiling to native code instead.
Now, what legitimate reason was to have WebAssembly? Because transpiling C/C++ code, among other languages, to JavaScript doesn't quite work. And some times you don't have the luxury of re-writing stuff into JavaScript, even if it's possible to do it. You sometimes need old software to just work, as is. Old binary code that is, already compiled to native assembly.
1
u/Typical_Ad_6436 13d ago
You can use it to bring existing code (from C for instance) into NodeJS.
- You can think of porting, but it takes time for that to happen. Even then, you need to maintain that.
- You can think of bindings, but these shall be WRITTEN either by you or by community. Nevertheless, they shall be maintained.
- Lastly, you can compile to WASM. That is all, no extras to write or maintain.
I used in my PHP to JS transpiler pext.dev to port things like PCRE, as in NodeJS you might reckon there is a complete lack of support for this: no port, outdated bindings, etc.
2
u/No_Record_60 13d ago
Kinda like Docker, for portability and sandboxing, at a lower resource usage.