[dojo-contributors] Dojo 1.7 Goals: has() and granular dependency lists

Rahul Akolkar rahul.akolkar at gmail.com
Tue Mar 15 19:42:42 EDT 2011


2011/3/14 Kris Zyp <kzyp at dojotoolkit.org>:
> On 3/14/2011 10:45 AM, ben hockey wrote:
>
> the ticket claims that using has like you've done is better for compression
> but can you point to an example or explain why it compresses better?  i
> haven't been closely following the thread about using closure with dojo 1.6
> - maybe it was somewhere in there?
>
> Here is the test I did. I took:
>
> define(["has"], function(has){
>     has.add("test", test);
>     if(has("test")){
>         alert("hi")
>     }
> });
>
> and compressed it (with closure, simple mode):
>
> define(["has"],function(a){a.add("test",test);a("test")&&alert("hi")});
>
> And then:
>
> define([], function(){
>     var features = {
>         "test": test
>     };
>     function has(feature){
>         return features[feature];
>     }
>     if(has("test")){
>         alert("hi")
>     }
> });
>
> and compressed it:
> define([],function(){({test:test}).test&&alert("hi")});
>
> And the latter was smaller.
<snip/>

Neat. Though there is a functional difference between the two that
allows the closure compiler to optimize away the has() function as a
property lookup -- whereas the latter requires all features based on
which has() branching may happen to be "added" within the same
define() above, the former does not. As a corollary, if its necessary
to branch on the same feature in different artifacts, then those
features will have to be added (and maintained) in each of those
artifacts/define()s when using the latter pattern.

-Rahul


More information about the dojo-contributors mailing list