reduce vs for loop recursive map

JavaScript performance comparison

Test case created by jbarngr

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  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);
  }

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

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

Compare results of other browsers

0 Comments