Use only the JavaScript that you need

I am a person who likes to use JavaScript development frameworks, such as Prototype or JQuery.  There is a time and a place for such things - as when developing your own website or working on some code that relies on lots of UI bells and whistles (fade outs, complex event handling, etc).   On the other hand, when I’m writing code that is going to end up as a distributed application, such as a widget,  I don’t think that using a development framework is appropriate.   There are a few reasons for this: 

  1. Serving only the code that is used saves user time - less downloading and less parsing.  
  2. Part of developing JavaScript applications is accepting the fact that you don’t know where your code is going to run.  You should maximize compatibility. 
  3. Frameworks make hard things easier to code - but not necessarily faster to run. 

I’ll start with the first point: serve only the code that you use.   Even when I’ve used a framework heavily on a project, it seems like there is about 60% or 70% of the framework that is useless to me.   Why should I bother with that crud?  Most of the time, it makes more sense to steal code from one framework or another to accomplish what I want to do.  More often than not, the problem I’m faced with can be stripped down to the statement: “I need a cross-browser way to do ‘x.’” If that’s all I need, I’d rather solve it myself through some simple research, or steal code from someone who has already done the research, such as PPK.   The ugly truth is that JavaScript developers need to think about bandwidth and processor overhead.   Not all users are running their browser on “decent” hardware and a fast connection.   There are no hardware requirements for JavaScript!  Sure, you need a  web browser with a JavaScript interpreter - but that includes such a large swath of browsers and versions of browsers that it’s literally a crap shoot.

Second point  - maximizing code compatibility.  I disagree with using a framework that pollutes the global scope in such a manner that it can break others’ scripts through its mere existence.  I am specifically referring to the Prototype library, which adds methods to native objects such as the Array object.   Of course, when you know where and how your code is running, use what you want.   I happen to think Prototype is a great library, and that the people working on it are incredibly smart.  On the other hand, if your code is supposed to run on a bunch of other sites, you should try to write it so that it doesn’t pollute and possibly break other scripts.   These days, there are tons of authoritative sites to find a cross-browser way to solve your particular problem. You can steal code from a framework,  Google the problem until you find a good snippet, or learn about the issue and come up with your own solution.  Using a library to solve only a few of these problems is heavy-handed if you are expecting a large amount of traffic on your particular project.  

Lastly - frameworks make hard things easy to code, but not necessarily faster to run.  For example, take the JQuery library.  It’s extremely useful to be able to query the DOM in the CSS/XPathy kind of thing that JQuery implements. On the other hand, if you are not concerned with the manner in which the library accomplishes what you ask it to do, you are asking for trouble.  The classic example is running a class name query on a large node set.  Yuck - that can take eons to complete in Internet Explorer!  Granted, it’s not JQuery’s fault that your code is inefficient - but if you had gone through the trouble of selecting all nodes and checking each class name to see if it matched what you were interested in, you’d quickly see that you were being inefficient.  When you use one of these libraries, you are tempted to use the concise, convenient syntax without caring how it works.  Understanding how it works, though, is the only way to understand how to use it efficiently.   

Safari 3.1 has embedded offline SQL storage

I just updated Safari 3.1 last night and noticed that one of the changes was to include an offline storage database for web applications.  This idea isn’t anything new - products like Google Gears, Flash, or Java DB allow web apps to store data in much larger chunks than cookies will allow already.  What I’m excited about is a possible standardization of this feature in the major browsers - and what that would mean for portability, speed, and reliability of web applications.   

What I’m wondering is what the possible implications of having a large, easily accessible store of data on a browser are.   I’m not very good at imagining up usages for stuff like this - still, I’m vaguely excited at the prospect of being able to access data on a client’s browser through SQL.

Unit testing JavaScript with JsUnit & Ant

Testing your JavaScript can be really easy - and that’s how it should be.  Before you start a JavaScript project, you should set up a system that allows you to run your tests very easily. Once you remove barriers to running your test suite (like having to click 5 times or hunt and peck for a file in a directory), you’ll find yourself running your tests more often.  Eclipse is a great IDE for developing with JavaScript for me.  I don’t particularly like Aptana, so I use the JSEclipse plugin from Adobe for syntax highlighting and checking and run builds with the integrated Ant view.   I’ve found a sweet spot when running unit tests on the JavaScript code I’m developing for InternetBroadcasting - currently I’m using an Ant task to run the JsUnit tests. The Ant task does the following things:

  1. Iterates over a list of browsers and creates a list of those that are present on the current machine (using a Groovy task)
  2. Runs the unit test suite in all of the browsers found
  3. Runs JsDoc Toolkit to generate javadoc-style documentation from code comments 
  4. Concatenates all the JavaScript files into one file, and puts it in a new folder (”dist”)
  5. Minifies the JavaScript using YUI Compressor

I love the fact that my tests run on Firefox, IE7 & 6, Safari 3, and Opera 9 on my local machine, and all I have to do is sit and watch.  During the deployment, the release manager runs the tests on a unixy machine which has a Firefox binary.   It’s handy to have one build file that works on any machine, regardless of the operating system or installed software (something that Java dorks are used to doing). At any rate, because I’ve automated the test runner on several browsers, and because I have a one-click way to run the tests,  I’m finding cross-browser bugs waaaay before I would have before I started using this technique.  

« Previous Page