Utilitying With Underscore.js

Underscore.js is a utility library which Backbone depends on. I’m afraid I don’t have a huge amount to say about it though because the only reason we’re using it is so that we can use Backbone. Lovely cuddly Backbone.

What is Underscore for?

It provides functional programming support for your JavaScript. This includes tools for manipulating Objects, Collections and Arrays as well as some generic utility functions.

It also supports some baisc HTML templating. This is the only feature I’ve actively used as it is valuable for producing Backbone Views. As a templater it’s pretty simple, which is probably a good thing. It’s been fit for all my extremely limited needs so far. This is tepid praise but based on inexperience rather than any negative feelings.

So why use Underscore?

So that we can use Backbone and because it makes life easier. A bit like functional jQuery.

Well that was short. Let’s talk about Lodash while we’re here. Lodash is a drop in replacement for Underscore. In theory this means that if you write your code base using Underscore, you can then change your mind and use Lo-Dash.

So Why Use Lo-Dash?

It’s a bit bigger but offers performance enhancements and some extra features. It’s also possible to generate specific subsections of Lo-Dash, so you can just get the bits relevant to Backbone and use that instead of the whole thing.
This link from the comments provides a much better answer to this question than I can. Lo-Dash has been designed to provide a more efficient, more consistent and better tested alternative to Underscore. It has been design with compatibility in mind though, so that if you don’t use the extra Lo-Dash features, the two should indeed be interchangeable.

We haven’t decided between the two yet. Lodash sounds pretty good but the whole thing is 19kb compared to Underscore’s 4kb. Taking just a Backbone version will reduce that but right now I don’t know how much. These enhancements sound great but are they 15kb of download better?
John in the comments has pointed out that the size difference is not as great I originally thought. It’s actually more like half a kilobyte.

The good news is since it drops in to replace, we should be able to build around underscore then swap in lodash to see if things are better.

Tagged , , , , ,

2 thoughts on “Utilitying With Underscore.js

  1. You’re comparing Underscore’s gzipped size to Lo-Dash’s non-gzipped size. Lo-Dash’s current gzipped size is 7.4kb (2.7kb more than Underscore). Lo-Dash’s `lodash underscore` build gzipped size is 5.2kb (less than 0.5kb more than Underscore).

    For more on the differences between Lo-Dash and Underscore check out the Stack Overflow response:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: