Multi Reducer Comparison

Benchmark created by Robert Stires on


Preparation HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Setup

const nodes = [];
  for (let i = 0; i < 100; i++) {
      const followers = [];
      for (let j = 0; j < 100; j++) {
          followers.push({ name: `ABC${i}${j}`, email: `abc${i}${j}@abc.com` });
      }
      nodes.push({
          name: `node${i}`,
          followers: followers
      });
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Sai's Example
const getSetOfFollowerEmails = (nodes) => {
  return _.uniq(nodes.reduce(
    (acc, node) => {
      node.followers.forEach(
        follower => {
          if (follower.email) {
            acc.push(follower.email);
          }
        }
      );
      return acc;
    },[]));
}
getSetOfFollowerEmails(nodes);
ready
Rob's Example
const followerReducer = (acc, follower) => {
  const email = follower.email;
  if(email){
    acc.push(email);
  }
  return acc;
};

const nodeReducer = (acc, node) => {
  acc.push(node.followers.reduce(followerReducer, []));
  return acc;
};

const getSetOfFollowerEmails = (nodes) => {
  return _.uniq(nodes.reduce(nodeReducer, []));
};
getSetOfFollowerEmails(nodes);
ready
Alex's Example
const getSetOfFollowerEmails = (nodes) => {
  return Object.keys(nodes.reduce(function(store, node) {
    return node.followers.reduce(function(store, follower) {
      follower.email && (store[follower.email] = true);
      return store;
    }, store);
  }, {}));
};
ready

Revisions

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

  • Revision 1: published by Robert Stires on