Comparing Dart, jQuery, CoffeeScript, CoffeeScript+jQuery, and JavaScript

UPDATE: Thanks to Ayose Cazorla we now have CoffeeScript+jQuery examples. And thanks to js2coffee.org we have vanilla CoffeeScript.

A neat comparison between jQuery and vanilla JavaScript hit Google+ via +Eric Bidelman, so I thought it would be fun to add vanilla Dart to the mix.

This is a fun experiment, but please don't base your decision to use one option or another based on this isolated blog post. Take a holistic approach to your evaluation.

Document is ready


// jQuery

$(document).ready(function() {
  // code…
});

// vanilla JavaScript

document.addEventListener("DOMContentLoaded", function() {
  // code…
});

// vanilla Dart

window.on.contentLoaded.add((e) => code);

// CoffeeScript+jQuery

$ -> code

// vanilla CoffeeScript

document.addEventListener "DOMContentLoaded", ->
  # code

Find all divs

// jQuery

var divs = $("div");

// vanilla JavaScript

var divs = document.querySelectorAll("div");

// vanilla Dart

var divs = document.queryAll("div");

// CoffeeScript+jQuery

divs = $ "div"

// vanilla CoffeeScript

divs = document.querySelectorAll("div")

Create a new div

// jQuery

var newDiv = $("<div/>");

// vanilla JavaScript

var newDiv = document.createElement("div");

// vanilla Dart

var newDiv = new Element.tag("div");

// CoffeeScript+jQuery

divs = $ "<div>"

// vanilla CoffeeScript

newDiv = document.createElement("div")

Add a class


// jQuery

newDiv.addClass("foo");

// vanilla JavaScript

newDiv.classList.add("foo");

// vanilla Dart

newDiv.classes.add("foo");

// CoffeeScript+jQuery

newDiv.addClass "foo"

// vanilla CoffeeScript

newDiv.classList.add "foo"

Toggle a class

// jQuery

newDiv.toggleClass("foo");

// vanilla JavaScript

newDiv.classList.toggle("foo");

// vanilla Dart

// doesn't work yet, see bug http://code.google.com/p/dart/issues/detail?id=1063
// newDiv.classes.toggle("foo");

// CoffeeScript+jQuery

newDiv.toggleClass "foo"

// vanilla CoffeeScript

newDiv.classList.toggle "foo"

Add a click handler for each <a>

// jQuery

$("a").click(function() {
  // code…
})

// vanilla JavaScript

[].forEach.call(document.querySelectorAll("a"), function(el) {
  el.addEventListener("click", function() {
    // code…
  });
});

// vanilla Dart

document.queryAll("a").forEach((el) {
    el.on.click.add((e) => code);
});

// CoffeeScript+jQuery

$("a").click -> code

// vanilla CoffeeScript

[].forEach.call document.querySelectorAll("a"), (el) ->
  el.addEventListener "click", ->
    # code

Append a new <p> to <body>

// jQuery

$("body").append($("<p/>"));

// vanilla JavaScript

document.body.appendChild(document.createElement("p"));

// vanilla Dart

document.body.nodes.add(new Element.tag("p"));

// CoffeeScript+jQuery

$("body").append $("<p>")

// vanilla CoffeeScript

document.body.appendChild document.createElement("p")

Add attribute to an element

// jQuery

$("img").filter(":first").attr("alt", "My image");

// vanilla JavaScript

document.querySelector("img").setAttribute("alt", "My image");

// vanilla Dart

document.query("img").attributes['alt'] = 'My image';

// CoffeeScript+jQuery

$("img:first").attr "alt", "My image"

// vanilla CoffeeScript

document.querySelector("img").setAttribute "alt", "My image"

Get parent node

// jQuery

var parent = $("#about").parent();

// vanilla JavaScript

var parent = document.getElementById("about").parentNode;

// vanilla Dart

var parent = document.query("#about").parent;

// CoffeeScript+jQuery

parent = $("#about").parent()

// vanilla CoffeeScript

parent = document.getElementById("about").parentNode

Clone an element

// jQuery

var clonedElement = $("#about").clone();

// vanilla JavaScript

var clonedElement = document.getElementById("about").cloneNode(true);

// vanilla Dart

var clonedElement = document.query("#about").clone(true);

// CoffeeScript+jQuery

clonedElement = $("#about").clone()

// vanilla CoffeeScript

clonedElement = document.getElementById("about").cloneNode(true)

Clear child nodes

// jQuery

$("#wrap").empty();

// vanilla JavaScript

var wrap = document.getElementById("wrap");
while(wrap.firstChild) wrap.removeChild(wrap.firstChild);

// vanilla Dart

document.query("#wrap").nodes.clear();

// CoffeeScript+jQuery

$("#wrap").empty()

// vanilla CoffeeScript

wrap = document.getElementById("wrap")
wrap.removeChild wrap.firstChild  while wrap.firstChild

Check if element has child nodes

// jQuery

if($("#wrap").is(":empty")) { ... }

// vanilla JavaScript

if(!document.getElementById("wrap").hasChildNodes()) { ... }

// vanilla Dart

if (document.query("#wrap").nodes.isEmpty()) { ... }

// CoffeeScript+jQuery

if $("#wrap").is(":empty")
  code

// vanilla CoffeeScript

code  unless document.getElementById("wrap").hasChildNodes()

Get next sibling element

// jQuery

var nextElement = $("#wrap").next();

// vanilla JavaScript

var nextElement = document.getElementById("wrap").nextSibling;

// vanilla Dart

var nextElement = document.query("#wrap").nextNode;

// CoffeeScript+jQuery

nextElement = $("#wrap").next()

// vanilla CoffeeScript

nextElement = document.getElementById("wrap").nextSibling

Next Steps

Like what you see? Learn more about Dart, its HTML libraries, and join the discussion. File a new issue if you see something missing.

Also, follow @dart_lang and +Dart Bits for more info!
20 comments

Popular posts from this blog

  • Sponsor:  Register today for  New Game, the conference for HTML5 game developers . Learn from Mozilla, Opera, Google, Spil, Bocoup, Mandreel, Subsonic, Gamesalad, EA, Zynga, and others at this intimate and technically rich conference. Join us for two days of content from developers building HTML5 games today. Nov 1-2, 2011 in San Francisco.  Register now ! This is the second article in a Box2D series, following the Box2D Orientation article. The Box2DWeb port of Box2D contains a nice example to show off the basics of integrating physics simulations into your web app. This post will provide a walkthrough of the example, explaining the high level concepts and code. First, let's see the example in action. The code for the above is open source and available on GitHub. It was adapted from Box2DWeb's example . Animating Before we look at Box2D, it's important to understand how the above simulation is animated. You might think setInterval or setTimeout is
    Keep reading
  • In which I port a snazzy little JavaScript audio web app to Dart , discover a bug, and high-five type annotations. Here's what I learned. [As it says in the header of this blog, I'm a seasoned Dart developer. However, I certainly don't write Dart every day (I wish!). Don't interpret this post as "Hi, I'm new to Dart". Instead, interpret this post as "I'm applying what I've been documenting."] This post analyzes two versions of the same app, both the original (JavaScript) version and the Dart version. The original version is a proxy for any small JavaScript app, there's nothing particularly special about the original version, which is why it made for a good example. This post discusses the differences between the two implementations: file organization, dependencies and modules, shims, classes, type annotations, event handling, calling multiple methods, asynchronous programming, animation, and interop with JavaScript libraries. F
    Keep reading
  • Angular and Polymer, sitting in a DOM tree, B-i-n-d-i-n-g. First comes components, Then comes elements, Then comes the interop with the node dot bind. Angular , a super heroic MVC framework, and Polymer , polyfills and enhancements for custom elements built on top of Web Components, can live harmoniously in the same app. This post shows you how to connect Angular-controlled components to Polymer-controlled elements via data binding. And we do it all in Dart . Angular and Polymer I get asked "Should I use Angular or Polymer?" a lot. My answer is, "Yes". That is, both libraries have distinct strengths, and you can use both in the same app. Polymer excels at creating encapsulated custom elements. You can use those custom elements in any web app or web page, regardless if that app is built with Angular, Ember, etc. Angular excels at application engineering, with dependency injection, end-to-end testability, routing, and services. Here are som
    Keep reading