News for hackers. Stuff that matters.
Re:How JavaScript works: inside the V8 engine by cm2187 on Monday August 21, 2017 @12:23PM (#15065650)
I was wondering, given that 90% of the javascript in browsers must be standard libraries (jquery, bootstrap & co) wouldn't it make sense for google to hash the source code for every published version of these libraries, compile those statically using full optimisation and ship the binaries as part of their updates to the browser, so that you only have to compile the idiosyncratic part of the code?
Re:How JavaScript works: inside the V8 engine by pcwalton on Monday August 21, 2017 @01:49PM (#15066235)
The fundamental problem is that interprocedural optimizations break that model. Optimizations that inline e.g. jQuery code into callers are very important. You end up needing to do optimization on jQuery either way.

Additionally, the code that JavaScript JITs generate is not position independent, and it contains no linkage information (symbol tables, etc.). It contains raw addresses of various browser objects within it. You would need to introduce some sort of linker for that to work, which is a lot of complexity.

Re:How JavaScript works: inside the V8 engine by jbergens on Wednesday August 23, 2017 @01:42AM (#15078812)
The js landscape changes really fast, they could probably never keep up.
Re:How JavaScript works: inside the V8 engine by flamedoge on Monday August 21, 2017 @03:45PM (#15067228)
doesn't mean it can't be done
Re:How JavaScript works: inside the V8 engine by pcwalton on Monday August 21, 2017 @06:01PM (#15068170)
It could be done, yes, but you'd have to weigh the benefits of caching against the performance downsides of linking and IR serialization. I wouldn't be surprised if it ends up not being worth it in practice.
Re:How JavaScript works: inside the V8 engine by fenomas on Monday August 21, 2017 @01:38PM (#15066155)
What are you suggesting "compile those statically using full optimization" would mean in this context?

The point of the article is that v8's optimizations work by watching the code execute. It can inline functions because it saw where they got called from; it can turn property accesses into fixed-offset lookups because it saw what object the accesses happened to, and so on.

(Unless you're talking about asm.js-style optimizations, but then the answer is presumably to use asm.js...)

Re:How JavaScript works: inside the V8 engine by dfox on Monday August 21, 2017 @02:30PM (#15066569)
Most of what jQuery does could be moved into standard browser APIs and then presumably implemented in native code. I feel that implementing it that way would certainly be significantly faster, because any performance loss from impossibility of trace-directed optimizations is outweighted by significant reduction of amount of crossings between native and JS code with completely different memory model. The fact that VM/native transition is expensive is exactly the reason why things like WebAssembly cannot directly manipulate DOM.

But this could not be implemented by writing C++ class that gets exported to JS code as $ when something that looks like jquery is loaded. In JS you can load jQuery and then monkey patch it, which will break when you use this kind of "transparent" optimization.

Re:How JavaScript works: inside the V8 engine by robin_reala on Tuesday August 22, 2017 @02:38AM (#15070240)
Most of what jQuery does could be moved into standard browser APIs and then presumably implemented in native code.

It pretty much has been. CSS syntax for selection lead to querySelectorAll, simpler AJAX calls to fetch, DOM manipulation functions were ported into the DOM spec wholesale.

Re:How JavaScript works: inside the V8 engine by cm2187 on Monday August 21, 2017 @02:56PM (#15066780)
I don't understand well enough dynamic language compilations to answer that but in principle, having 1 billion devices consuming CPU cycles to JIT recompile the same javascript paths over and over seems horribly wasteful.
Re:How JavaScript works: inside the V8 engine by chrisseaton on Monday August 21, 2017 @03:06PM (#15066861)
> recompile the same javascript paths over and over

The person you are replying to's point is that they aren't the same paths, when you take into account the other code and data in the rest of the application, which effects how the code even in the shared libraries is compiled.

Re:How JavaScript works: inside the V8 engine by cm2187 on Monday August 21, 2017 @03:12PM (#15066925)
They are mostly the same path, like if I select something based on an ID to add a function as an event handler, it is the same path, just with a different argument.

I don't know, I am outside of my area of confidence here. But in a world where the technological advances in computing have less to do with increasing computing capacity than reducing power consumption, whether on mobile, laptops or servers, the jquery library that needs recompiling on every execution feels like the non-recyclable plastic bag of computing.

Re:How JavaScript works: inside the V8 engine by chrisseaton on Monday August 21, 2017 @03:21PM (#15066987)
> like if I select something based on an ID to add a function as an event handler

Nah it's more complicated than that in reality (I work on dynamic languages and have a PhD in them). You just want to select based on an ID, but in order to make that operator fast, JavaScript would normally pass information about what you want to select, what possible values it could have, whether it's allocated on the heap or you somehow stored it somewhere else, and a whole host of other things.

You could say 'well don't do those optimisations then' but then you just have slow unoptimised code and the point was to make this fast.

Re:How JavaScript works: inside the V8 engine by jFlatz on Monday August 21, 2017 @12:30PM (#15065717)
I feel like this is one of those things that I have been thinking to my self for a little while now. "Why hasn't someone already solved this problem." In my mind there is no reason for my browser to download 26 copies of the same jquerry file.
Re:How JavaScript works: inside the V8 engine by cpeterso on Monday August 21, 2017 @01:02PM (#15065928)
Here is a proposal to use the hashes from <script> elements' Subresource Integrity attributes to cache JavaScript. There are some concerns about leaking users' browsing history based on resource load times.

https://hillbrad.github.io/sri-addressable-caching/sri-addre...

Re:How JavaScript works: inside the V8 engine by ChrisSD on Monday August 21, 2017 @01:01PM (#15065920)
If [websites] use a common CDN then this can be avoided.

[edited, with thanks to delinka]

Re:How JavaScript works: inside the V8 engine by delinka on Monday August 21, 2017 @01:13PM (#15065995)
If the sites you're visiting use a common CDN then this can be avoided.
Re:How JavaScript works: inside the V8 engine by tracker1 on Monday August 21, 2017 @03:16PM (#15066959)
This was going to be my comment as well.. I tend to prefer cdnjs for as much as possible, since it seems to be the most diverse CDN in terms of supported libraries.

Though I've been trending away from using most libraries separately and bundling... may need to think about breaking things out again. For the most part, however, most of my bundle sizes are pretty small. React in dev, Preact for prod is the biggest mix... checking bundle size before/after adding a module (some will add a huge dependency tree).

There's no substitution for being judicious... and imho, even if you have to look it up frequently, using the DOM is usually better than loading in jQuery... even if you center around Array.from(document.querySelectorAll('...')) as your main usage.

Re:How JavaScript works: inside the V8 engine by nitwit005 on Monday August 21, 2017 @01:55PM (#15066283)
The order of scripts matters, so it's not really possible except for the first script (similar to C/C++ #include optimizations). Even then, you'd have to deal with the possibility of someone modifying something the scripts create.
Re:How JavaScript works: inside the V8 engine by Vinnl on Tuesday August 22, 2017 @02:09AM (#15070135)
Decentraleyes [1] does something like that, although it still fetches them from a common CDN. That said, I'm not sure how well it works: there are many, many different versions of every different library around, and when they're bundled, you don't get to see them separate anymore at all.

[1] https://decentraleyes.org/

Re:How JavaScript works: inside the V8 engine by erikpukinskis on Monday August 21, 2017 @01:00PM (#15065912)
Adding an entire new layer of complexity seems like a bad tradeoff. You'd break the debugger. You'd break the security model.
Re:How JavaScript works: inside the V8 engine by ndesaulniers on Tuesday August 22, 2017 @12:49AM (#15069801)
Simply hashing these wont work since the scripts may be minified by different tools. Also, there's lots of different versions of these libraries, so we're talking about a fairly large number of precompiled libraries.
Re:How JavaScript works: inside the V8 engine by chrisseaton on Monday August 21, 2017 @12:30PM (#15065716)
You wouldn't be able to optimise the standard libraries very well without any information about the program calling them and the data they are using, though. I guess it would turn out to be a lot slower in practice.
Re:How JavaScript works: inside the V8 engine by sand500 on Monday August 21, 2017 @12:26PM (#15065674)
Maybe ship popular libraries as web assembly?
Re:How JavaScript works: inside the V8 engine by ndesaulniers on Tuesday August 22, 2017 @12:50AM (#15069807)
No, web assembly does not support dynamic typing that is used by many functions in these libraries. Dynamic typing may not be fast, but it is useful.
Re:How JavaScript works: inside the V8 engine by sjrd on Monday August 21, 2017 @12:33PM (#15065735)
Right, because wasm is there to solve all our performance problems, isn't it?

And how exactly is your jquery.wasm going to access the DOM? Or expose methods to your application, for that matter?

wasm is not a drop-in replacement for JS.

Re:How JavaScript works: inside the V8 engine by markdog12 on Monday August 21, 2017 @01:07PM (#15065958)
There are plans for WebAssembly modules to directly access the DOM: https://github.com/WebAssembly/gc/blob/master/proposals/gc/O...
Re:How JavaScript works: inside the V8 engine by Fifer82 on Monday August 21, 2017 @12:48PM (#15065833)
I really don't know much about wasm.

I just wondered though, from a birds eye view, does using wasm effectively give you control of everything within the "frame" of your browser?

Can I get rid of the DOM(boilerplate aside), get rid of CSS?

For example HTMLCanvasElement is a flat surface. I can do what I want within it as a scene graph renderer.

Could I do that with wasm? Could I invent my own 2D entities (x, y, width, height). My own 2D Constraints resize like Apple Devices etc?

Re:How JavaScript works: inside the V8 engine by skybrian on Monday August 21, 2017 @01:00PM (#15065918)
It's still in the same sandbox as JavaScript. You don't get any new API's. There's only going to be speedup on CPU-intensive things.
Re:How JavaScript works: inside the V8 engine by flavio81 on Monday August 21, 2017 @09:19PM (#15069033)
> wasm is not a drop-in replacement for JS

It is not a "drop-in" replacement in any way, but I'm pretty sure it will eventually replace JS completely.

Re:How JavaScript works: inside the V8 engine by Spivak on Monday August 21, 2017 @12:46PM (#15065819)
No, but if we're being honest, it's exactly what everyone wants it to be. A bytecode for the web.
Re:How JavaScript works: inside the V8 engine by adamnemecek on Monday August 21, 2017 @02:06PM (#15066374)
Eventually the Dom might be replaced with some webgl renderer.
Re:How JavaScript works: inside the V8 engine by mhh__ on Monday August 21, 2017 @12:27PM (#15065692)
Could be done, although it would have to be done carefully.
Re:How JavaScript works: inside the V8 engine by slackingoff2017 on Monday August 21, 2017 @12:30PM (#15065718)
If ES6 used hashes to version libraries this might be possible. I still think you'd have too many versions to keep track of.

In other spaces the pendulum is swinging away from shared libraries since storage space and bandwidth are relatively cheap.

Java is often distributed as "fatjars" these days. IOS and Android apps bundle their dependencies the same way as far as I know. The whole serverless movement is a move away from shared code for better or worse.

Re:How JavaScript works: inside the V8 engine by colordrops on Monday August 21, 2017 @01:00PM (#15065916)
I'm thinking it's because they already have mounds of tech debt that would better address long term issues if they had the time to work on it. Take a look at the chromium bug tracker. It's overwhelming.