views:

337

answers:

8

Hi,

I like Google Web Tookit API approach. It use Java language behind the scenes that compiles ONLY JavaScript code WHOSE TARGET BROWSER NEEDS. It happens some developers would like to use that feature in pure JavaScript language.

Anwser: WHAT COULD WE SUGGEST in order to fullfill this requirement ?

I suggest to use JavaScript comments (as a flag) as a way some compiler (like Yahoo JavaScript compiler) analises our app JavaScript code and generates only a JavaScript Framework code needed.

Example: a hypothetical JavaScript framework (JQuery, Mootools, Prototype ect) code

// depends function say
funcion sayHello() {
    // some code   
}

function say() {
   // some code
}

// more and more no needed Javascript framework functions in our app

So when my app use a function sayHello, only that sayHello function and its dependencies would be filtered through JavaScript comments, nothing else. So, this way our application would be lighter, by using only JavaScript Framework code needed.

And you: what do you suggest ?

+9  A: 

If the JavaScript code of the framework is served as a cacheable file then the download cost of requesting the entire framework (e.g. jQuery.js) can be eliminated, but if you were generating the framework code on the fly (as you suggest above) then it's going to be harder to take advantage of caching.

On top of this the memory cost of defining the entire framework is probably unlikely to be problematic (assuming the framework is written sensibly).

So, pulling in the entire framework, as is the common case, is simple, works well and doesn't require a particular server-side infrastructure (like GWT does).

wrumsby
Agree, dynamic recompilation will not be good here, but you can do this as pre-deployment activity and have the final output be a compiled .js file which will pull all the necessary script parts
Jimmy Chandra
cache is king, just like in life
scunliffe
I think what he actually is referring to re: GWT is not that it compiles it on the fly (which would be way too slow and resource intensive), rather it refers to how GWT creates different JS files for each browser, and will then send the appropriate one to IE, FF, Opera, etc. based on which UA is detected.
Alex JL
A: 

If you are making your own framework from scratch, just make different script files for each feature and modularize development. You will know exactly what features are where, so you only need to include the features you need.

As to libraries already created, its just easier to include the whole thing than for users to have to know exactly what features are in what part of the script. The library really doesn't have tons of overhead with getting included and getting "compiled." You would need to do evaluation to firgure out what was getting used and what was not. If you did all this before you deployed the page it would be fine. You would do this separately with each page ,which you might need to do, since each page would use different features. But don't try to use more js to try to figure out what features are used, because that would have to be transmitted and just defeat the purpose.

CrazyJugglerDrummer
+2  A: 

I don't think it would be worth it to add the complexity of selected inclusion. jQuery is 57k and loads very fast. Your energy is much better spent worrying about other things that might actually matter.

pbreitenbach
You are right, pbreitenbach. But you can not forget that as a new JavaScript version is launched (and new features), more code is needed.
Arthur Ronald F D Garcia
+5  A: 

There's one case where it would make sense to me--a jQuery for iPhone. It doesn't make any sense for all the non-Safari baggage in jQuery to be in there slowing things down.

For the typical desktop, though, if you get a JS library from the Google APIs, it's likely already cached and ready to go.

Nosredna
Not sure why this got voted down. There are actually alternatives to jQuery for the iPhone because of the excess baggage. And it's been discussed on the jQuery discussion boards. The iPhone is a slower machine than a PC. See XUI.
Nosredna
Also, IIRC the iPhone browser can only cache ~ 25 kB of data.
wrumsby
+1 for mentioning getting your libraries through Google API
Gab Royer
+2  A: 

I would suggest learning to program in JavaScript and understanding the various peculiarities of the different DOM implementations, then writing just the code necessary for your application. If you really don't want to have to deal with re-creating all the event handling shenanigans and so on, then nick the relevant techniques from the libraries. You'll learn a lot more about what you're doing that way, as you'll need to actually understand how it all works to be able to integrate those techniques with your application.

Having worked professionally with JavaScript since 1996 I, like many, was initially tempted by the apparent ease of use offered by libraries; but if I see one more answer on here that says "use jQuery" (followed by some code that isn't even optimal in jQuery) when the correct answer is to use an existing and well-documented feature of JavaScript that works on every single implementation since Netscape Navigator 3, I'll scream ;-)

NickFitz
A: 

I don't know about other frameworks, but with Dojo Toolkit you can take advantage of caching better by using a public CDN copy of the source in your site, from AOL or Google.

Wahnfrieden
A: 

A nice article about it is found here:

The New Yahoo! Front Page

regards,

Arthur Ronald F D Garcia
+1  A: 

I'm currently doing something like this with my javascript code base, but only because I'm building a very large single-page ajax application. In comments at the top of the page I do java style import statements to include other javascript "classes"/objects. This is then used to create a single javascript file from the dozens of files in the application, pulling in only the code that is needed.

That said, it's not being done to reduce a small library like jquery into an even smaller library for just the things needed. When it comes to that stuff, I think caching or a CDN should probably be good enough.

Russell Leggett