reduce vs for loop recursive map

Benchmark created by jbarngr on


Setup

var basictree = {
      id: 0,
      countMe: true,
      children: [
          {
              id: 1,
              countMe: false,
              children: [
                  {
                      id: 2,
                      countMe: true,
                      children: [
                          {
                              id: 3,
                              countMe: false,
                              children: [
                                  {
                                      id: 4,
                                      countMe: true,
                                      children: [
                                          {
                                              id: 5,
                                              countMe: false,
                                              children: [
                                                  tree = {
                                                      id: 6,
                                                      countMe: true,
                                                      children: [
                                                          {
                                                              id: 7,
                                                              countMe: false,
                                                              children: []
                                                          }
                                                      ]
                                                  }
                                              ]
                                          }
                                      ]
                                  }
                              ]
                          }
                      ]
                  },
                  {
                      id: 8,
                      countMe: true,
                      children: [
                          {
                              id: 9,
                              countMe: false,
                              children: [
                                  {
                                      id: 10,
                                      countMe: true,
                                      children: [
                                          {
                                              id: 11,
                                              countMe: false,
                                              children: [
                                                  tree = {
                                                      id: 12,
                                                      countMe: true,
                                                      children: [
                                                          {
                                                              id: 13,
                                                              countMe: false,
                                                              children: [
                                                                  {
                                                                      id: 14,
                                                                      countMe: true,
                                                                      children: []
                                                                  },
                                                                  {
                                                                      id: 15,
                                                                      countMe: false,
                                                                      children: []
                                                                  },
                                                                  {
                                                                      id: 16,
                                                                      countMe: true,
                                                                      children: []
                                                                  },
                                                                  {
                                                                      id: 17,
                                                                      countMe: false,
                                                                      children: []
                                                                  },
                                                                  {
                                                                      id: 18,
                                                                      countMe: true,
                                                                      children: []
                                                                  },
                                                                  {
                                                                      id: 19,
                                                                      countMe: false,
                                                                      children: []
                                                                  },
                                                                  {
                                                                      id: 20,
                                                                      countMe: true,
                                                                      children: []
                                                                  },
                                                                  {
                                                                      id: 21,
                                                                      countMe: false,
                                                                      children: []
                                                                  },
                                                                  {
                                                                      id: 22,
                                                                      countMe: true,
                                                                      children: []
                                                                  }
                                                              ]
                                                          }
                                                      ]
                                                  }
                                              ]
                                          }
                                      ]
                                  }
                              ]
                          }
                      ]
                  }
              ]
          }
      ]
  };
  
  
  
  
  var tree = {
    id : 999999,
    countMe: true,
    children : []
  }
  
  for (var i = 0; i < 1000; i++) {
  tree.children.push(basictree);
  }

Test runner

Ready to run.

Testing in
TestOps/sec
reduce
var counterReduce = function(a) {
                var elf = 0;
                if (a.children.length !== 0) {
                    elf = elf + (a.countMe ? 1 : 0) + a.children.reduce(function(p, v) { return p + counterReduce(v);}, 0);
                } else {
                    return a.countMe ? 1 : 0;
                }
                return elf;
            }
counterReduce(tree);
ready
for
var counterFor = function(b) {
                var elf2 = 0;
                var numberOfChildren = b.children.length;
                if (numberOfChildren !== 0) {
                    for(var count = 0; count < numberOfChildren; count++) {
                        elf2 = elf2 + (b.countMe ? 1 : 0) + counterFor(b.children[count]);
                    }
                } else {
                    return b.countMe ? 1 : 0;
                }
                
                return elf2;
            }
counterFor(tree);
ready

Revisions

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

  • Revision 1: published by jbarngr on