On my previous post I looked at how some frameworks handle the multimodule approach to separate javascript code. Since then I’ve been playing with different ways of loading scripts online vs. doing it offline.

After doing some research on the subject I found this website where they show three different approaches to load javascript files. I did some testing and here are some results. The three methods to load the javascript tested were:

Inserting a script tag with a src into the head of the document:

var loadScriptTag = function(src) {
    var head = document.getElementsByTagName("head")[0];        
    var script = new Element("script", { type: "text/javascript" });
    script.src = src;
    head.appendChild(script);
};

Inserting a script tag with code fetched using ajax into the head of the document:

var loadScriptTagWithAjax = function(src) {
    var response = new Ajax.Request(src, {
        method: "get",
        evalJS: false,
        asynchronous: false
    });

    var head = document.getElementsByTagName("head")[0];        
    var script = new Element("script", { type: "text/javascript" });
    script.text = response.transport.responseText;
    head.appendChild(script);
};

Using eval to execute the code fetched using ajax:

var loadScriptAjax = function(src) {
    var response = new Ajax.Request(src, {
        method: "get",
        evalJS: false,
        asynchronous: false
    });
    eval(response.transport.responseText);
};

The test code was the following:

var log = function(m) {
    new Insertion.Bottom(document.body, m + "<br />");
};
           
log("<h1>loadScriptTag</h1>");
log("Before");
loadScriptTag("test-tag.js");
log("After");

log("<h1>loadScriptTagWithAjax</h1>");
log("Before");
loadScriptTagWithAjax("test-tag-with-ajax.js");
log("After");

log("<h1>loadScriptAjax</h1>");
log("Before");
loadScriptAjax("test-ajax.js");
log("After");

This files were loaded using a local Apache server. The results were the following:

Firefox 3.1 Beta 2 on Mac

loadScriptTag
Before
After

loadScriptTagWithAjax
Before
Test loadScriptTag
Test loadScriptTagWithAjax
After

loadScriptAjax
Before
Test loadScriptAjax
After

It seems that firefox executes the script with src just before the script with text everytime. Any way it is asynchronous.

Safari 3.2 on Mac

loadScriptTag
Before
After

loadScriptTagWithAjax
Before
Test loadScriptTag
Test loadScriptTagWithAjax
After

loadScriptAjax
Before
Test loadScriptAjax
After

Safari works just like firefox.

Opera 10 Alpha on Mac

loadScriptTag
Before
After

loadScriptTagWithAjax
Before
After

loadScriptAjax
Before
Test loadScriptAjax
After
Test loadScriptTag
Test loadScriptTagWithAjax

Opera executes the script tags when all the code is done running.

IE 6.0 on Mac using ies4osx

loadScriptTag
Before
After

loadScriptTagWithAjax
Before
Test loadScriptTagWithAjax
After

loadScriptAjax
Before
Test loadScriptAjax
After
Test loadScriptTag

IE executes the script tag with src at the end but the script tag with content in the right place.

Overall the only common result is using ajax with eval. This is my choice for modularjs.

Grab here the sources for these tests

When I’m developing small or medium or large javascript projects I end up with the same problem all the time: code grows and it starts getting a mess if you don’t do something about it. The only way out is separating your code into different modules which leads to the only conclusion of separating it on different files.

Most javascripts frameworks use different approaches to this. Some use an offline builder to create a single javascript file they distribute. Some other use a online dependency loading through different methods and some others a combination of both. Here’s a small list of frameworks and what they do:

YUI Loader Utility

If you plan to use the YUI Loader Utility, it means that you’ll have to add it to your page, in other words 9.4kb more, not too bad. You can load any javascript file. It also supports rolled up files, this means that you can have all the source files “rolled up” on one file and the loader works exactly the same way as if you were using separate files. This is an online only loader. It looks like this:

var loader = new YAHOO.util.YUILoader({
    require: ["colorpicker", "treeview"],
});
loader.insert();

For custom modules it looks like this:

loader.addModule({
    name: "json",
    type: "js",
    fullpath: "http://www.json.org/json2.js",
    varName: "JSON"
});

Dojo Toolkit

This choice means that you’ll have to build over dojo and use dojo’s require and provide mechanisms to request and provide modules online. It also has a ant and rhino based build system that can pack offline all modules in one file. The base dojo script takes ~100kb compressed which is quite large. I’m not really sure if the build system would work without the base dojo.js file. It looks like this:

dojo.require("dijit.form.Button");

And the module file, in this case by default at dijit/form/Button.js, should start with:

dojo.provides("dijit.form.Button");

Prototoype

The Prototype build system uses Rake. This approach has the benefit of having multiple source javascript files and through a build system combine them on a single file. The downside is that you cannot choose at runtime which modules to load or not making it a all-or-nothing package. Also while developing you’ll need a ruby server to be able to test your code without compiling it. The code you need looks like this:

<%= include 'lang/class.js', 'lang/object.js' %>

Note that it is not javascript but ruby code.

Mochikit

The Mochikit build system is based on python and rhino. It’s an offline packer and for development you’ll have to add all the scripts you are developing and dependencies on the right order. It has some sort of validation of import like this:

MochiKit.Base._module('Format', '1.5', ['Base']);
...
MochiKit.Base._exportSymbols(this, MochiKit.Format);

In the next article I’ll write about the internal methods used to load javascript files online, their advantages and disadvantages as well as my own way of doing it.

I’m starting a new project. The problem I need to address is now glass cutting and how to optimize a set of cuts to satisfy an order of cut glass sheets. The algorithm I used is the following:

  1. Get the largest piece of material from the order.
  2. Get the smallest piece of material from the available material that can contain the ordered piece.
  3. Cut.
  4. Repeat the process.

It seems to be working just fine for a large set of examples. I cannot find some example where it would fail to produce the best result. There were some constraints I had to take into account.

  • Cuts should always be done from side to side on the available material and the cuts should be done perpendicular to the largest of the sides of the material whenever possible.
  • Each cut should be independent of the previous cuts and sequential in case there’s a glass breaking or anything else. In that case the cuts should be able to be “replayed” from the failure point forward after doing the correction.

Here’s an image of the result.

The 10th cut as calculated by sectum.

The 10th cut as calculated by sectum.