184 points · by stablemap · 8y ago · 72 comments
Edit: Apologies for wording this poorly. Clearly there is a need for performant JavaScript. My question is more about how much we expect WebAssembly to take over from what JavaScript is currently used for. Thank you for all the insightful comments!
Yes, but how much of the browser code out there is is truly performance critical.
For a lot of code out there it is important that it perform well enough but it won't see much benefit from WebAssembly because of bottlenecks elsewhere (the DOM, the user, the network). Keeping the maintainability benefits JS currently has over WA is a bonus worth paying a small performance price, but you still want that price to be as small as practically possible.
As WA and the toolchanins for which it is the final compile target mature, this may change. But I suspect JS has a place, a place where performance considerations are often not insignificant, for some time to come, with WA for extra performance in truly critical areas (i.e. proper number crunching).
That said, I’m actually skeptical wasm will ever find much of a use case with the DOM. The browser itself is already heavily optimized for working with the DOM, so wasm is unlikely to be competitive in speed except for specific use cases.
If you are writing JS today with even a couple years of experience, it's guaranteed to be used for everything until you retire (well, if retiring from a company after putting in your 20 was still a thing).
With WebAssembly we now have a much more reasonable target for far more modern (and productive and enjoyable) languages like purescript.
Type inference is useful but there’s tooling to add that.
Erlang and JavaScript come much much later on in the evolution of programming languages; Erlang originated in the late 1980s, and JavaScript in 1995.
I think Erlang may have been the first production platform to do it, but I'm not sure about that. I suspect the idea has been around for a very long time, and there's probably some obscure language out there from 1960s IBM mainframes that came up with the idea.
For example, if you want to do anything involving strings, you have to convert it to a typed array. So that is invoking charCodeAt for every character and saving it to an index in such an array. If you want to get a string out, you have to convert uint charcodes to single-char strings and append them together one at a time. The overhead of these things alone probably makes WASM useless for almost all code involving string manipulation (that also interacts with the rest of the browser).
That is not to say that WASM is useless for strings either: if you have one huge string, only need to convert it to bytes once, and then do repeated operations on the resulting data within WASM, there is a bigger chance that it works out. For example, parsing source code: one conversion to WASM is enough, the rest can be handled from within WASM. Fast search using a prefix trie also sounds plausible: the trie can be built up within WASM, and for searching one only needs to return the indices of matching substrings within the original string. That is a lot less overhead.
Anyway, my pint is: yes, we definitely need performant JavaScript. And I wouldn't be surprised if faster WebAssembly leads to faster JavaScript as well: sorting Typed Arrays used to be so slow that a custom radix sort implemented in JavaScript beat the built-in sort by 4-10x. Somewhere in the last five versions of Chrome the Uint32Array and Int32Arrays got an enormous speed boost. My guess is: because of the demand of faster interop with WASM, something under the hood was improved, directly or indirectly leading to faster sorting as well.
TextDecoder looks fine, but I'm a bit wary of using TextEncoder naively. It requires allocating a Uint8Array for each string. Each Uint8Array comes with 200 bytes of overhead[0]. Depending on your algorithm you can end up with thousands if not millions of tiny strings. While even phones will probably have enough RAM to handle a few hundred megabytes these days, that will slow things down simply from an allocation/garbage collection perspective.
Furthermore, typed array allocation used to be incredibly slow compared to array allocation - the gap is smaller now but it is still slow enough to be a bottle-neck in my code.
The quote below is from a V8 dev replying to someone who opened an issue about this slow allocation. It is from 2012, but it still applies:
> The reason is simply that while constructing Array is done exclusively in the Javascript virtual machine and an allocation in the VM's heap, constructing a TypedArray involves the browser binding (at least in Chrome) and allocation outside the VM's heap. The former can be properly optimized, while the latter cannot. Future optimizations in this area are already on our radar, but have rather low priority.
> Generally speaking, typed arrays are good if you want to allocate a long-living dense array with a certain type and size. Array objects are better if you want to create temporary arrays often.
I hope things will improve now that Typed Arrays finally get some proper love thanks to WASM.
[0] https://stackoverflow.com/questions/45803829/memory-overhead...
JS has also become nice to work with since ES6. Incredibly easy to set up a working environment, amazing debugging capabilities, you can just change a few lines and instantly get to see your result by pressing F5, etc. The only really big downside is that static typing is missing, but right now I'd rather not have static typing than go through the additional hoops of setting up transpiler builds. I'd probably switch to typescript if it was natively supported by browsers and V8, but JS is just fine till then.
Agreed. I've found that using JSDoc with an IDE that supports validating against it is the best of both worlds.
Since we're trolling now, there is plenty worse around, like Python(the only language that manages to have worse scoping rules then pre-ES6 javascript, which is quite a feat), Cobol and LUA. JS ES6 is just fine. The trick is not to use the crap parts.
DOM access to WebAssembly will come.
https://github.com/WebAssembly/design/labels/Phase%201%3A%20...
Even if it did, you'd still lose a lot of productivity with it if you're used to working with the high level of abstraction of the DOM, e.g. line wrapping.
There are certainly cases where dropping to GLSL is the only way to get a UI to work at all, but most of the time the metadata-providing feature of HTML is far more important than squeezing some milliseconds of performance.
WebAssembly is not much faster than JS, harder to develop and debug, and requires even more extra tooling.
It’s a useful technology but it’s never going to be the primary choice for application development on the web - merely a tool in the toolbox.
WebAssembly is for the nieche languages ;)
The user base of those languages that wants to do this is incredibly small right now
^ I 'grok' this, but is there not a more proper technical term for this given the tone?
not complaining.. just wondering