Can Backbone.JS use Lodash.JS instead of Underscore.JS ?

The fact is – Lodash.JS is nice competitor for Underscore.JS. There are so many thoughts in a community regarding how different and how good they are. This post is about Backbone.JS and required dependency of Underscore and my willingness to BE SURE, that replacement/migration is OK. So I asked myself:

How good Lodash is against Underscore in scope of Backbone.JS stable/correct work?

1 2

And as turned out, I found more than answers, I found “future”. 🙂

First of all

I think that John-David Dalton (@jdalton) SHOULD NOT HAVE forked and create new wheel. And instead he should have contributed to Underscore.JS and help Jeremy Ashkenas (@jashkenas) with improving Underscore.JS.

WHAT would be BAD in such collaboration? But I know – none will listen to this pattern, because of money and competition. And besides, I may just simply not know the history of both.

Anyway. Undefined parts.

According to Backbone docs we can do it with no risk:

Mimics of the Underscore and jQuery APIs, such as Lo-Dash and Zepto, will also tend to work, with varying degrees of compatibility.

But this is not very confident message/statement, I would say, there is hidden level of uncertainty. In backbone.JS we can easy replace dependency of underscore.JS by replacing it with lodash.JS. So if you free to change it, you can do it. I was free to do it – but again, I asked myself – is it correct? Then I made simple research over the Internet. Here is info from Lodash site:

lodash works great with Backbone & is run against Backbone’s unit tests on every commit.

Besides simple lodash, there is lodash.undescore version, and lodash-compat, which are as candidates to be used during migration. Differences between lodash and underscore resource gives us a comment about list of proven differences between underscore and lodash:

  • _.flatten in underscore is deep by default and you have to pass true as second argument to make it shallow. In lodash it is shallow by default and passing true as second argument will make it deep!
  • _.last in underscore accepts a second argument which tells how many elements you want. In lodash there is no such option. You can emulate this with .slice.
  • _.first (same issue)
  • _.template in underscore can be used in many ways, one of which is providing the template string and data and getting HTML back (or at least that’s how it worked some time ago). In lodash you receive a function which you should then feed with the data.
  • _(something).map(foo) works in underscore, but in lodash I had to rewrite it to, foo).

There (Differences between lodash and underscore) is also mention about performance:

lodash performing 100-150% faster than underscore in even simple, native functions such as Array.every in Chrome.

And this resource [2014] tells us more about performance. In fact results based on data, as I see, so here is just snapshot: underscore_vs_lodash   Besides “Lodash and Underscore and their difference analysis”, I took 2 versions of Backbone.JS and ran tests with different Lodash.JS versions.

Backbone 1.1.2 and

  • lodash 3.10.1 – no failed tests
  • lodash.compat v3.10.0 – no failed tests
  • lodash from my prj. frozen v. 2.4.1 – no failed testes
  • lodash.underscore v2.4.1 – no failed tests:

Backbone 1.2.1 and

  • lodash 3.10.1 – no failed tests
  • lodash.compat v3.10.0 – no failed tests
  • lodash from my prj. v.2.4.1 – 4 failed tests (68, 69, 200, 202)
  • lodash.underscore v2.4.1 – 20 failed tests (68, 69, 200, 202, 342-345, 355-363, 366, 368, 370)

So I assume, that using “latest versions of libraries on the release moment” of both of them is appropriate and not risky. And obviously – using different releases/versions is risky.

And now good parts 🙂

There is discussion about Merging Underscore and Lodash started by Jeremy (@ashkenas) in May this (2015) year. Based on outcome by Jeremy (Jun-2015) and by John-David (July-15-2015) there is upcoming UNDERDASH 🙂

And now I start like and respect more these two guys 🙂
NEVERTHELESS, back to my original question, I have no official opinion on migration to lodash.
So I asked Backbone.JS community 🙂 #3746

Outcome (see details in github issue.):

Running backbone tests shows me, that Backbone and Lodash can be used together. And both communities seems to be ok with migration. 

PS. BTW, regarding _.template() I faced with issue in my project (Lodash 2.4.1 and Underscore 1.7.0 with Backbone 1.1.2):

  • lodash.template DOESN’T WORK THE SAME as underscore.template.

It was the only one code change I needed. And this is rather sign of “my project codebase outdated” than 3rd party lib fault.

This code works fine for underscore BUT not work for LODASH:

var template = _.template(templateHTML, {'helloVar': 1234});

And this code works for both:

var template = _.template(templateHTML);
$('.myElement').html(template( {'helloVar': 1234} ));





Leave a Reply

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

You are commenting using your 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