This is an idea that I’ve been kicking around for a while.
Back in the C days, when most everything was determined at compile time, life was simple. You wrote your program, you preprocessed it, you compiled it, then you linked it. Linking is the cool part. The linker would determine exactly which bits of code you were using, and selectively remove everything else. This dramatically decreased final executable size and improved efficiency. As a side effect, if you called a function that wasn’t defined in your code, the linker produce an error about “unresolved symbols,” which would remind you to link to your favorite library.
Those of you in the know will probably say “Just turn on server-side gzip compression” or “200 KB is only downloaded once, then the browser caches it.”
exec() would be trouble, not to mention classes. There are lots of things to work out, and there would be limitations.
I have been searching to see if this has already been done (a common phenomenon with ideas that I have). It took a while†, but I did locate a project called jslinker. From what I gather, jslinker was a summer of code project from 2006 (the last commit seems to have been 2006, so it may be abandoned). I’m not sure what it’s capabilities are, but it’s at least proof that this can be done (or has been tried before).