push.apply vs recursion

Benchmark created on


Preparation HTML

<script>
  var McDuffFamily = {
      name: "Jack McDuff I",
      children: [
          {
              name:"Jack McDuff II",
              children: [
                                {
                                        name:"Jack McDuff III",
                                        children: [
                                                {
                                                        name:"Jack McDuff IV"
                                                },
                                                {
                                                        name:"Jonh McDuff I",
                                                        children: [
                                                                {
                                                                        name:"Jonh McDuff I",
                                                                        children: []
                                                                }
                                                        ]
                                                }
                                        ]
                                },
                                {
                                        name:"Shena McDuff"
                                }
                        ]
          },
          {
              name:"Poor bastard",
              children: [
                                {
                                        name:"Citzen I",
                                        children: [
                                                {
                                                        name:"Darth Vader"
                                                }
                                        ]
                                },
                                {
                                        name:"Citzen II",
                                        children: []
                                }
                        ]
          }
      ]
  };
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Put the name of all family member in a array using recursion
function getNames(o) {
  names.push(o.name);
  if (o.children) 
    for (var i = 0; i < o.children.length; i++)
      getNames(o.children[i]);
}
names = new Array();
getNames(McDuffFamily);
ready
Put the name of all family member in a array using push.apply
obj = [McDuffFamily];
var names = [];
for (i = 0; i < obj.length; i++) {
  obj[i] && obj.push.apply(obj, obj[i].children)
  names .push(obj[i].name);
}
ready

Revisions

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