Ramda vs. Lodash

JavaScript performance comparison

Revision 4 of this test case created by Scott Sauyet

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.19.1/ramda.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.6.1/lodash.min.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;
  }
  
  var lodash = function(data) {
    return _.filter(
      _.map(_.filter(_.map(data, 'counter'), isOdd), square), 
      lessThanThreeDigits
    );
  };
  
  var ramda1 = function(data) {
    return R.filter(lessThanThreeDigits,
      R.map(square,
        R.filter(isOdd,
          R.pluck('counter', data))));
  };
  
  // Longer but more maintainable.
  var ramda2 = R.pipe(
      R.pluck('counter'),
      R.filter(isOdd),
      R.map(square),
    R.filter(lessThanThreeDigits)
  );
  
  var nativeFunctional = function(data) {
    return data
      .map(function(value) {
        return value.counter;
      })
      .filter(function(value) {
        return value != null;
      })
      .filter(isOdd)
      .map(square)
      .filter(lessThanThreeDigits);
  };
  
  var nativeImperative = function(data) {
    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);
        }
      }
    }
    return result;
  };

};
</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
Lodash
lodash(data);
pending…
Ramda without relying on currying or composition
ramda1(data);
pending…
Ramda with currying and composition
ramda2(data);
pending…
Native functional
nativeFunctional(data);
pending…
Native imperative
nativeImperative(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