3 ms·
I think a large contributor to this trend is a side effect of JavaScript, which is to perform library inclusion at runtime inside the browser. Think about it.
by zeta0134 9y ago
I think a large contributor to this trend is a side effect of JavaScript, which is to perform library inclusion at runtime inside the browser.
Think about it. If you include a gigantic library in a compiled language, but only use 3 of its features, the rest of the library doesn't make it into your final program. Only those three features that you used do, because that's the linker's job. It takes only the features of a given library that are actually used for space efficiency reasons, and removed anything unneeded.
This is possible for JavaScript, but common language usage patterns work against this goal. If you're going to use a clientside library that's fine, but you should ship only the small fraction of it that your code actually calls into, not the entire honking library, 90% of which goes untouched by the client for every user.
Point here is, I agree with you, but this is a difficult problem to solve. You're asking a large number of frankly inexperienced programmers to deviate heavily from most JavaScript tutorials, and worse, most example code included in the documentation of most large frameworks.
- jdmichal 9y agoThat's standard dead code elimination, and that's only true in static linking. Dynamic linking, which is far more popular, will load the entire library into memory. And then you get interesting hybrids like Java, where libraries are dynamically linked but only the classes that are actually used are loaded. [0] The equivalent for dynamic languages is tree shaking, and is a harder or impossible problem depending on the language. When you can do something like the following in JavaScript, how do you determine what's dead or alive? var fn = document.getElementById("function").value; var input = document.getElementById("input").value; var output = functions[fn](input); document.getElementById("output").value = output; [0] EDIT: But all the method and fields within a class are loaded, regardless of whether they are used. Which is why I called it a hybrid.
- Spivak 9y agoLanguage theory question: In the case of JS is there a safe, but still useful, subset of the language where dead code elimination could be improved? Ignore if it can be detected or not, we'll pretend that the author has to certify that they followed your subset and didn't do X, Y, or Z. If they disobeyed your rules then all bets are off and the result is undefined.
- majewsky 9y agoI'm afraid it won't help much to restrict the language. For optimal dead-code elimination (aka tree shaking), you need human-level intelligence. Here's an example I posted yesterday in the thread about iOS app bloat: https://news.ycombinator.com/item?id=14618324 https://news.ycombinator.com/item?id=14618324