Multi Reducer Comparison

JavaScript performance comparison

Test case created by Robert Stires

Preparation code

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>
      
<script>
Benchmark.prototype.setup = function() {
  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
      });
  }

};
</script>

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
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);
pending…
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);
pending…
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);
  }, {}));
};
pending…

Revisions

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

0 Comments