Ramda vs. Lodash

JavaScript performance comparison

Revision 2 of this test case created by Yuri and last updated

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.17.1/ramda.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  var data = _.range(10000).map(function(i) {
    return {
      counter: i
    }
  });
  function isOdd(num) {
    return num % 2 === 1;
  }
  function square(num) {
    return num * num;
  }
  function lessThanThreeDigits(num) {
    return num.toString().length < 3;
  }

};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Native functional
var result = result = data
  .map(function(value) {
    return value.counter;
  })
  .filter(function(value) {
    return value != null;
  })
  .filter(isOdd)
  .map(square)
  .filter(lessThanThreeDigits);
pending…
Native imperative
var result = [];
var length = data.length;
for (var i=0; i<data.length; i++) {
  var value = data[i].counter;
  var squared;
  if (isOdd(value)) {
    squared = square(value);
    if (lessThanThreeDigits(squared)) {
      result.push(squared);
    }
  }
}
pending…
Lodash
var result = _.filter(
  _.map(_.filter(_.pluck(data, 'counter'), isOdd), square), 
  lessThanThreeDigits
);
pending…
Ramda without relying on currying or composition
var result = R.filter(lessThanThreeDigits,
  R.map(square,
    R.filter(isOdd,
      R.pluck('counter', data))));
pending…
Ramda with currying and composition
// Longer but more maintainable.
var result = R.pipe(
  R.pluck('counter'),
  R.filter(isOdd),
  R.map(square),
  R.filter(lessThanThreeDigits)
)(data);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments