[dojo-contributors] Module formats for the browser

James Burke jburke at dojotoolkit.org
Mon Mar 29 12:27:06 EDT 2010

On Mon, Mar 29, 2010 at 5:53 AM, Kris Zyp <kzyp at dojotoolkit.org> wrote:
> AFAICT, basically both loaders just insert script elements to load
> scripts, so I wouldn't expect much difference in performance (maybe some
> one knows better). The primary difference between labjs.com and
> RequireJS is that LABjs is designed for loading scripts that don't
> declare their own dependencies, so one has to manually declare
> dependencies and loading order with LABjs. RequireJS is designed for
> modules that do declare their dependencies, which obviously matches
> Dojo's design.

RequireJS can load other script files that do not participate in the
require.def() or require() ways of specifying dependencies. However,
those scripts can be executed in a different order than specified in
the require([]) call that is used to load those other scripts.

LABjs has a distinguishing characteristic to help this: it will fetch
scripts using a script tag with type="script/cache", so it can fetch
the scripts, and it is OK if they are received out of order because
the browser will not execute them. Then, once all of them are loaded,
another round of script tags is added with type="text/javascript",
with the idea that the modules should be in cache now and will execute
in the right order.

The type="script/cache" could also be used to fetch files that you may
want to execute later, but want them immediately available.

It is a neat trick, but I have been wary of it. It assumes too much on
the caching behavior of browsers, IMO, and LABjs also has an XHR
branch instead of using the script/cache since it is better in some
cases, apparently. Kyle and I talked about the differences on one of
my blog posts, here is his reply about the XHR branch (see full post
for full discussion):

I am not sure I would want to take the hit of adding the extra code
paths to support that type of script/cache thing. There are common use
cases where ensuring script execution order is valuable, for instance,
trying to load jQuery and plugins that rely on jQuery's presence all
at the same time.

However, I would rather tackle that problem by patching jQuery to
include RequireJS, or advocating the nested calls in dev:
require(["jquery"], function(){
    require(["plugin1", "plugin2"], function(){});

It is slightly slower in dev for this path, but not slow enough to
make the dev suffer, and with the optimization tool in RequireJS,
after a build, you will load all of those files in just one HTTP
request, which would be better than using LABjs in production. But I
encourage you to read the comment thread in the link above to get
Kyle's perspective.


More information about the dojo-contributors mailing list