This morning, Underscore.js went to version 0.4.2. As a brief reintroduction, Underscore is a library of functional helper methods that fits in neatly alongside jQuery. It now has an option to use OO-style syntax, as well as sequences of chained operations on collections. Here's an example of chaining in action:
var lyrics = [
{line : 1, words : "I'm a lumberjack and I'm okay"},
{line : 2, words : "I sleep all night and I work all day"},
{line : 3, words : "He's a lumberjack and he's okay"},
{line : 4, words : "He sleeps all night and he works all day"}
];
_(lyrics).chain()
.map(function(line) { return line.words.split(' '); })
.flatten()
.reduce({}, function(counts, word) {
counts[word] = (counts[word] || 0) + 1;
return counts;
}).value();
=> {lumberjack : 2, all : 4, night : 2 ... }
The library has benefitted immensely from the opinions gathered on Hacker News from an earlier posting, and I wanted to see what y'all think of this addition.
The Array prototype methods are proxied through the chained object, so you can slip in a "reverse" or a "concat" without breaking your chain.
Finally, some speed comparisons with jQuery have been added to the bottom of the testing and benchmark page. Because Underscore proxies to the browser-native implementation, doing an "each()" on a thousand-element array is over 5 times faster than the jQuery equivalent in Safari, and over 3.5 times faster in Firefox. Your mileage may vary, so you can use that page to run your own tests in your favorite browser.
Underscore.js: http://documentcloud.github.com/underscore
Speed Tests: http://documentcloud.github.com/underscore/test/test.html
Development Code: http://documentcloud.github.com/underscore/underscore.js
Minified Code (2k gzipped, fits on a screen): http://documentcloud.github.com/underscore/underscore-min.js