Skip to content

Instantly share code, notes, and snippets.

@vvgomes
Last active August 10, 2021 18:10
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 10 You must be signed in to fork a gist
  • Save vvgomes/451ea5ca2c65e87c92e4 to your computer and use it in GitHub Desktop.
Save vvgomes/451ea5ca2c65e87c92e4 to your computer and use it in GitHub Desktop.
Ramda vs Lodash
var _ = require("lodash");
var R = require("ramda");
var companies = [
{ name: "tw", since: 1993 },
{ name: "pucrs", since: 1930 },
{ name: "tw br", since: 2009 }
];
var r1 = _(companies).chain()
.filter(function(c) {
return c.name.split(" ")[0] === "tw";
})
.map(function(c) {
return {
name: c.name.toUpperCase(),
since: c.since
};
})
.sortBy(function(c) {
return c.since;
})
.reverse()
.value();
console.log("with lodash:", r1);
var r2 = R.compose(
R.reverse,
R.sortBy(R.prop("since")),
R.map(R.over(R.lensProp("name"), R.toUpper)),
R.filter(R.where({ name: R.test(/^tw/) }))
)(companies);
console.log("with ramda:", r2);
@omeid
Copy link

omeid commented Feb 18, 2018

@hillerstorm: Got you covered!

var r5 = (companies || [])
  .filter(c => c.name && c.name.startsWith("tw"))
  .map(c => ({ ...c, name: c.name.toUpperCase() }))
  .sort((a, b) => b.since - a.since);

@marano
Copy link

marano commented Oct 11, 2018

Even though Ramda is definitely more powerful, and I do prefer Ramda over lodash, I've found that for a lot of common operations lodash is simpler to use. It handles many real world cases that Ramda doesn't. For instance, when you iterate object properties with lodash it will skip "hidden" properties (that start with _) by default. It also performs much better on some operations, of course it doesn't really matter most of the time.

@hnordt
Copy link

hnordt commented Apr 28, 2019

An even shorter Ramda version:

const r3 = R.pipe(
  R.filter(R.where({ name: R.startsWith('tw') })),
  R.map(R.evolve({ name: R.toUpper })),
  R.sort(R.descend(R.prop('since')))
)

r3(companies)

@enoh-barbu
Copy link

enoh-barbu commented Dec 16, 2019

in the lodash example you said c.name.split(" ")[0] === "tw" but in the ramda's one you've put a regex R.test(/^tw/) . Really?
The same regex could be also applied in the first case, natively /^tw/.test(name) which is actually shorter.

@vvgomes
Copy link
Author

vvgomes commented Dec 16, 2019

But that would not be point-free. The point is not being shorter. The point is being point-free, auto-curried, composable.

Thanks for adding to the discussion :)

@enoh-barbu
Copy link

Oh, I wasn't aware of this comparison.

@qjnz
Copy link

qjnz commented Jan 12, 2021

how about from performance perspective? anyone has done it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment