News for hackers. Stuff that matters.
Re:Why am I excited about WebAssembly? by stevebmark on Sunday July 17, 2022 @12:37PM (#32128326)
Fun fact: Wasm can be be significantly slower than vanilla Javascript https://betterprogramming.pub/how-fast-is-webassembly-versus...
Re:Why am I excited about WebAssembly? by vbezhenar on Sunday July 17, 2022 @12:56PM (#32128537)
IMO the main selling point of WASM is its predictable performance.

It's not a secret that you can write JS which will be JIT-ed into an extremely efficient machine code. But it's a "secret" - how to write that JS. You would need seriously advanced hackers who can study V8 assembly output and correlate it with used JS features. And do it all the time, when someone changes that code. Or may be even unrelated code changes will change the way V8 compiles that particular code snippet. JS compilation is black magic.

On the other hand, writing C is boring and solved problem. Compiling C to wasm works. It's predictably fast. You can use it for performance-critical code and it'll probably work without any adventures to V8 internals.

Re:Why am I excited about WebAssembly? by hajile on Sunday July 17, 2022 @04:04PM (#32130680)
Making fast JS isn't some super-secret thing. Sure, there are weird cases like `x = y > z ? y : z` being 100x faster than `x = Math.max(y, z)`, but most performance improvements come from three simple rules:

1. Create an object with a fixed number of keys and NEVER add keys, remove keys, or change the data type of a key's value.

2. Make arrays of a set length and only put ONE type data type inside. If that data type is an object or array, all the objects/arrays must have the same type

3. Functions must be monomorphic (always called with the same parameters in the same order of the same type)

Do this and your code will be very fast. Do something else and it will get progressively slower.

Running the profiler in Chrome or Firefox is very easy and it will show you which functions are using up most of your processing time. Focusing on applying these rules to just those functions will usually get you most of the way there.

Re:Why am I excited about WebAssembly? by bastawhiz on Sunday July 17, 2022 @07:20PM (#32132553)
... Until it's not. If your code needs to be predictably fast, it can't be fast the majority of the time, it needs to be fast all the time. It's not about painting your updates in a tenth of a second, it's about painting your video game scene in under 16ms, every time.

There's nothing you can do to guarantee your JS isn't passed inputs that trigger pathological cases. There's no linter that can guarantee that you're writing code in a way that is the fastest it can be (even with type checking!). Asking developers to be a human linter for the sake of consistent performance is a bad developer experience no matter your skill level.

WASM simply doesn't have this problem.

Re:Why am I excited about WebAssembly? by chii on Monday July 18, 2022 @08:21AM (#32137446)
> There's nothing you can do to guarantee your JS isn't passed inputs that trigger pathological cases. ... WASM simply doesn't have this problem.

why doesn't this problem also apply to WASM?

Re:Why am I excited about WebAssembly? by zwarag on Monday July 18, 2022 @04:22AM (#32135860)
How did you find out? Just by profiling or did you read some book/article about that topic?
Re:Why am I excited about WebAssembly? by int_19h on Sunday July 17, 2022 @07:13PM (#32132511)
i.e. basically use JavaScript as if it were a statically typed language.
Re:Why am I excited about WebAssembly? by hajile on Sunday July 17, 2022 @07:53PM (#32132814)
Kinda. As long as you guard the boundaries of the parts that need performance, you can be a lot more flexible everywhere else. You see this in libraries like React where the external API is polymorphic, but they tend to pass through to calls that are monomorphic internally so performance is better.

I wish TypeScript helped more here. I'd prefer if it had a performance option that disallowed or at least warned about these kinds of things.

Re:Why am I excited about WebAssembly? by int_19h on Sunday July 17, 2022 @11:20PM (#32134252)
I wish we just ditched the JS legacy and had a properly statically typed language, with dynamism as a layer on top (like e.g. "dynamic" in C#), rather than underpinning anything.

Which is what wasm will, hopefully, give us in the long run. And ensure that said PL will have to remain competitive against the new contenders, since they can always replace it.

Re:Why am I excited about WebAssembly? by hajile on Monday July 18, 2022 @09:20AM (#32138025)
I think the reverse option is better. Add a `use types` directive to functions or modules. Inside those modules, use a very strict, structurally-typed ML-style type system that ensures good performance. If an untyped piece of code calls a typed one, coerce primitives and throw on objects that aren't a structural match.
Re:Why am I excited about WebAssembly? by ArrayBoundCheck on Sunday July 17, 2022 @04:00PM (#32130640)
> IMO the main selling point of WASM is its predictable performance.

Not once in my entire life have I heard anyone say this

Almost always its either about it being faster or so they can use a language that isn't JS. And both are those have dubious value because I seen wasm be slower and people complain a lot about lack of tool support. Which is why the other day I claimed very few people use it. I seen many try it once or twice and not want to go through it again

Re:Why am I excited about WebAssembly? by duped on Sunday July 17, 2022 @02:00PM (#32129279)
The other thing is that the semantics of JS force some constraints on the JIT that make it harder for it to optimize code aggressively. Specifically, JIT compilers for JavaScript need to implement dynamic de-optimization when optimized code paths turn out to be wrong (because JS can do things like overwrite a method, meaning inclined calls to the method are now invalid).

Afaict it's much easier to write a high performance JIT for WASM because those cases aren't possible. And consequently, it's easier for something compiling to WASM to get high performance out.

Re:Why am I excited about WebAssembly? by thrashh on Sunday July 17, 2022 @02:06PM (#32129354)
Doesn’t it depend on the source language? If you compile JS to WASM, you’d have the same problem.

I imagine Python performance isn’t too great either.

Whereas if you compiled (or translated — not sure how comparable the WASM instruction set is) x86 bytecode to WASM, it’d be a walk in the park.

Re:Why am I excited about WebAssembly? by duped on Sunday July 17, 2022 @09:40PM (#32133557)
Sure, but that's the problem of the hosted language's compiler and not the JIT. Then the optimization pass turns into compiling the redefined method to WASM which can then be JIT compiled and inlined, so deoptomization isn't as bad (at least only the compilation unit has to be ditched)
Re:Why am I excited about WebAssembly? by no_wizard on Sunday July 17, 2022 @01:32PM (#32128983)
I agree with this. One string use case I’ve seen is number crunching. Doing complex math via WASM is fast and predictable and supports a wider variety of float / integer types.

Another use case I’ve toyed with is date time. Specifically trying to figure out if something like the rust Chronos crate is a better fit for crunching and calculating dates than something like date-fns or Luxon. Not sure about this one yet.

Re:Why am I excited about WebAssembly? by zamadatix on Sunday July 17, 2022 @02:03PM (#32129316)
With Temporal on the horizon I wouldn't bother. Even if Temporal doesn't do what you want wrapper libraries that do will be very lightweight in the crunching regard https://tc39.es/proposal-temporal/docs/cookbook.html#arithme...
Re:Why am I excited about WebAssembly? by no_wizard on Sunday July 17, 2022 @04:27PM (#32130891)
This assumes two things though, and this is another point I just realized about WASM that I like, which is for (most) modern browsers have asm.js / WASM support, and it goes back much farther than Temporal. Therefore with Temporal we have to consider the following:

1. Browser support - its not there yet. you'd have to polyfill. A production level polyfill is 16 KB, and is still very nasacent, and, on top of that, requires support also for BigInt[0]. The polyfill that tc39 put out is decidedly marked as non-production ready[1].

2. Polyfilling - as mentioned above, we have to deal with polyfilling the API, and that isn't a clear and easy story yet. WASM support goes back farther than this.

3. Size - its entirely possible to get WASM builds under 16 KB, and the support is better, espcially for operations on strings and numbers (dates fit this category well). The only complication I haven't quite solved yet is:

A) Can I validate that a WASM build will be under 16 KB. This is crucial. I'd even accept it at 20 KB because of wider browser support[2]

B) Can I fall back to asm.js if needed (there is a slim range of browsers that support ASM.js but not WASM, mostly pre-chromium Edge[3]

C) Is it performant compared to something like Luxon or date-fns? WASM excels at string / numerical operations so my sneaking suspicion is yes, at least in terms of the WASM operations. The complexity will be serializing the operations to a JS Date instance, Luxon & the Intl API might be most useful here

[0]: https://github.com/fullcalendar/temporal/blob/main/packages/...

[1]: https://github.com/tc39/proposal-temporal#polyfills

[2]: https://caniuse.com/wasm

[3]: https://caniuse.com/asmjs

Re:Why am I excited about WebAssembly? by zamadatix on Sunday July 17, 2022 @05:47PM (#32131702)
Yeah if you need something ready for prod by a literal next month or something Temporal definitely isn't it as the API isn't locked yet.

Don't forget WASM doesn't provide direct access to any OS time APIs (timezone info, current time, regional time change modifications) so the solution will still basically boil down to "call Date() and polyfill a better library" except now you have extra code to ferry the data back and forth to do a few string and math ops. Unless the use case is processing very large datetime datasets in one call the JS<->WASM function call overhead for all of this will probably take the majority of the execution time.

Not to mention after you get all of this solved, tested, and deployed you know as soon as Chrome starts shipping Temporal the cool custom solution becomes 50% slower for the average user despite all the effort because you didn't just use something like a Luxon which automatically updated to use Temporal on release. This may just be me being lazy though :p.

Re:Why am I excited about WebAssembly? by no_wizard on Sunday July 17, 2022 @06:52PM (#32132296)
Even if Temporal ships tomorrow, its minimum 5 years for most applications to take advantage of it, so you're either polyfilling with feature detection or waiting it out using libraries like `date-fns` or `luxon` to fill the gap.

strings & numbers are WASMs strong point, so if you can pack the locale information tightly in a binary format, you might actually win out in the medium term. This shouldn't be a years long project by any means. And frankly, with the way enterprises move, you'll always have some client (at least in my business) where I need to support some modernish browser that may not have Temporal, so if this is more performant (we do alot of date time datasets, so yes, thats part why I'm looking at this) why not?

It could also be the wrong solution. I'll found out one way or another.

Re:Why am I excited about WebAssembly? by zamadatix on Sunday July 17, 2022 @07:20PM (#32132551)
Just the IANA timezone database is ~400 KB gzipped for the data only and it's really quite the project to actually parse correctly. With that you'll still need to ferry Date(), Intl(), and friends for getting the current info about the user into the WASM module. Only then can you actually start talking about the code which competes with the 20 KB JS polyfill which started with all of the above as precompiled native code and data.

WASMs strong point isn't necessarily "strings and numbers" it's running large amounts of compiled code on large amounts of data. Video processing, PDF readers, video games. As an example even computing a large image the Mandelbrot fractal (pure math workload) then passing back an arraybuffer of the pixels was faster in JavaScript until WASM SIMD+Threads finally landed and JavaScripts poor parallelism finally factored in. Doing it with a functional call per pixel JavaScript is still ahead of even WASM with SIMD due to the functional call overhead.

But all that said I think it's a really cool project to try and I hope you're able to build what you're seeking. If you do be sure to post it to HN so I can check out how you managed to pull it off :).

Re:Why am I excited about WebAssembly? by cornstalks on Sunday July 17, 2022 @01:40PM (#32129069)
I’m gonna rephrase some of a previous comment[1] I made when someone else posted benchmarks like that:

That is comparing WASM+JavaScript vs pure JavaScript. Unsurprisingly there’s some interop overhead. Those benchmarks are not relevant if you’re not using JavaScript (e.g. WASI stuff) or you’re doing the bulk of your calculations in WASM and not rapidly jumping back and forth between WASM and JavaScript.

[1]: https://news.ycombinator.com/item?id=30157954

Re:Why am I excited about WebAssembly? by flohofwoe on Sunday July 17, 2022 @03:16PM (#32130142)
The first benchmark isn't measuring the performance of WASM, but the calling overhead from JS to WASM (which is pretty small, but it's still an optimization barrier for the JS engine).

The second benchmark isn't measuring the performance of Javascript, but the performance of the Javascript sort() call (which most likely is implemented as native code).

In general, WASM should be both in the same ballpark as portable natively compiled code (e.g. not using SIMD), as well as Javascript which has been written for performance (which also means that well written - but non-idiomatic - Javascript can be in the same ballpark as portable native code).

The main advantage of WASM versus JS isn't mainly performance, but predictable performance (because the GC is taken out of the picture, and the linear memory model), and that WASM is a better compilation target than JS.

Re:Why am I excited about WebAssembly? by comonoid on Sunday July 17, 2022 @12:54PM (#32128511)
I don't see WASM is slower than vanilla JavaScript. I see that calling WASM from JavaScript has certain overhead.
Re:Why am I excited about WebAssembly? by nikki93 on Sunday July 17, 2022 @02:10PM (#32129390)
The main benefits are being able to use the same codebase for native and web builds and also not needing to use a GC and being able to do memory management yourself. The latter stuff really makes a positive difference for Wasm in eg. graphics / games. Can be less pronounced if the app mainly does DOM manipulation potentially due to the API boundary.
Re:Why am I excited about WebAssembly? by enos_feedler on Sunday July 17, 2022 @12:44PM (#32128413)
This might be a fact, but it certainly isn’t fun or interesting. Anybody who is framing WebAssembly into some sort of competition with javascript is completely missing the point of it’s existence. The major problem with WebAssembly right now is the major point of it’s existence is mirky. It will take time and effort from everyone using it for figure out it’s true calling, but I am hopeful and optimistic this gets discovered. In the meantime it doesn’t help to pit it against javascript.
Re:Why am I excited about WebAssembly? by thrashh on Sunday July 17, 2022 @02:12PM (#32129419)
It’s not like we haven’t been down a similar path

Java introduced a language and library… but the real innovation was that it introduced a cross platform VM that was supported by some organization

WebAssembly is now doing the same thing… but just the cross-platform VM part

Now you can run Python and JS on the JVM these days but these are not de-facto implementations and so their adoption is pretty low. I wonder if the same issue will apply to these alternate WASM implementations of existing languages.

Re:Why am I excited about WebAssembly? by EGreg on Sunday July 17, 2022 @01:16PM (#32128797)
If you can use emscripten to compile all stuff to Javascript, then what’s the point of WASM? Smaller instruction set? It’s a serious question. All the JS has the same properties, it seems, as WebAssembly. The runtime implementers can reason about it better?
Re:Why am I excited about WebAssembly? by zamadatix on Sunday July 17, 2022 @01:40PM (#32129074)
You can use chopsticks to eat Jello but that doesn't mean you will be happy with it (well maybe that would actually be fun but that's beside the point ;)). Emscripten to JavaScript works by taking what's available in JavaScript and turning it into a base to compile to. Then the hope is the JavaScript engine can figure out what you were trying to do originally and optimize accordingly. Emscripten to JavaScript was never a "this is a great way to do this!" more a "this is the only way to do this!".

WASM was an evolution to say rather than do all that why not just have a way to tell the browser's VM what we want to do directly. Now instead of having to parse JS syntax to find type hints and so on the browser can just parse pre-encoded bytecode. Instead of having to understand certain logic is trying to emulate functionality like 64 bit integer multiplication and optimize it out the browser can be told to do a 64 bit integer multiplication directly. Since this is a separate interface from JavaScript it allows work on things like threads, SIMD, and garbage collection to not worry about how JavaScript has a hard time with these concept since JavaScript is not the base anymore.

Re:Why am I excited about WebAssembly? by Findecanor on Sunday July 17, 2022 @04:32PM (#32130936)
Sure, but the way browsers' Javascript engines get their high performance is complex.

JIT-compilation with optimisation (and de-optimisation!) is costly, so browsers tend to only interpret Javascript the slow way at first, enabling each (higher) tier of compilation only after run-time profiling. With higher complexity comes higher risk of errors, and there have been a number of serious vulnerabilities in browsers' Javascript JIT-compilers in the last decade.

Not many companies have the resources to develop a high-performance Javascript engine that can compete with the best.

Also, writing optimised Javascript code so that it gets made into fast JIT-compiled code is a black art.

WASM on the other hand, has been designed so that it could be assembled into machine code straightforwardly in a single pass using little CPU time. You'd get native performance straight away. (Not that optimising WASM runtimes don't exist)

Re:Why am I excited about WebAssembly? by jsheard on Sunday July 17, 2022 @01:36PM (#32129035)
https://hacks.mozilla.org/2017/03/why-webassembly-is-faster-...

WASM has continued to evolve past what's possible in JS/asmjs since that article as well, with things like SIMD support

Re:Why am I excited about WebAssembly? by davidmurdoch on Sunday July 17, 2022 @01:45PM (#32129125)
So it is about performance over JavaScript.
Re:Why am I excited about WebAssembly? by zamadatix on Sunday July 17, 2022 @02:17PM (#32129462)
Performance against JavaScript for specific use cases (i.e. being a general purpose VM for traditional application code) is a factor. Performance against JavaScript in general (e.g. calling the same simple function in a loop) is not a factor.

E.g. if you had only done rendering on a CPU and someone came by and said "we can do all sorts of stuff we couldn't do before with this GPU check it out!" it'd be easy to say "I could do all that on a CPU" and you could even show the exact same benchmarks presented here and then say "see, the CPU even runs the single threaded factorial function many more times per second than this new GPU". Everything you said would be absolutely correct in the most literal form yet it'd still be completely missing the point of why the GPU was made and how to assess if it fits that purpose better.

Then someone shows you the GPU doing rendering it was designed to do well better than the CPU and the reply is "So it is about the GPU being faster than the CPU?". Yes. No. It depends what context you're asking from. Traditional use cases no, what it was designed to do well yes.

The original article which talked about WASM being slower itself specifically notes this relation of purpose, functionality, and performance it's just tucked away in the conclusion:

> definitely don’t go converting all your websites’ JavaScript to WebAssembly! However, that’s not really the aim of WebAssembly. Its aim is to enable richer experiences on the web that require higher performance, for example machine learning, virtual reality, or gaming.

Re:Why am I excited about WebAssembly? by RodgerTheGreat on Sunday July 17, 2022 @01:34PM (#32129014)
In a nutshell, WASM is slightly more efficient than Empscripten + Asm.js because it has a binary format and circumvents the need to parse a blob of JS. That's about it, really.

Whereas Asm.js had (has?) perfect backwards-compatibility with unsupported browsers and JS interpreters, WASM requires users to remain on the bleeding edge of new browser features as it continues to evolve, and introduces a whole host of fantastic new bottlenecks as the designers puzzle over how to interface WASM modules with the rest of the facilities JS can already access.

The whole thing is a hilarious boondoggle- an insane amount of effort and complexity for mild bandwidth and page-load time savings- made all the more hilarious for the fact that a remarkable number of people seem unaware that Asm.js ever existed in the first place.

Re:Why am I excited about WebAssembly? by torginus on Sunday July 17, 2022 @01:43PM (#32129101)
Aren't they though? Is it not possible, that in the future web pages will be scripted in literally every programming language, from python to rust, and javascript will be just one of many ways to write a webapp?
Re:Why am I excited about WebAssembly? by smegsicle on Sunday July 17, 2022 @12:54PM (#32128519)
what should it be compared against if not javascript?
Re:Why am I excited about WebAssembly? by zamadatix on Sunday July 17, 2022 @01:20PM (#32128844)
Any general purpose VM is fine, including the VMs behind JavaScript, but just double check that's actually what's being compared instead of inter JavaScript and VM performance. How many times per second you can call 1-5 line functions from a webpage's execution context is comparing the latter and as the article notes at the end:

> definitely don’t go converting all your websites’ JavaScript to WebAssembly! However, that’s not really the aim of WebAssembly. Its aim is to enable richer experiences on the web that require higher performance, for example machine learning, virtual reality, or gaming.

WASM functions aren't meant to replace small JS functions on your standard website. It's meant to be a general purpose VM you can target large amounts of non-webpage code to.

Re:Why am I excited about WebAssembly? by enos_feedler on Sunday July 17, 2022 @03:10PM (#32130062)
Its a new thing not trying to supplant anything else.
Re:Why am I excited about WebAssembly? by torginus on Sunday July 17, 2022 @02:06PM (#32129365)
I think that article needs some work - for one, it admittedly didn't account for the startup time WASM needs to compile the bytecode. Second, I'm not familiar with AssemblyScript - but I wouldn't be surprised if its performance wasn't up to something like C++, and some of the benchmarks test stuff like the builtin sort which depends massively on the quality of the standard library.
Re:Why am I excited about WebAssembly? by maxgraey on Monday July 18, 2022 @10:18AM (#32138685)
The article is relatively old. But I have the most complaints about the performance measurements. The WebAssembly should be tested in a way that eliminates the interop time, which is very expensive. That's why sorting and simple multiplication will always be slower if tested outside WebAssembly. Sorting in AssemblyScript is actually faster than in Rust or JavaScript: https://twitter.com/MaxGraey/status/1414867216676368384
Re:Why am I excited about WebAssembly? by svantana on Sunday July 17, 2022 @02:57PM (#32129911)
Not refuting the results, but that has got to be the worst, laziest benchmarking I've seen in a long time.