nesting vs loop

JavaScript performance comparison

Test case created by RobG

Info

Compare the speed of nested functions vs a plain loop.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    function getMaxSomething(data) {
      var max = -Infinity;
      for (var p in data) {
        if (data.hasOwnProperty(p)) {
          if (data[p].Number_of_Something > max) {
            max = data[p];
          }
        }
      }
      return max;
    }
   
    function getMaxSomething2(data) {
   
      var max = Math.max.apply(null, Object.keys(data).map(function(e) {
        return data[e]['Number_of_Something'];
      }));
      return max;
    }
   
   
   
    var data = {
      0: {
        'Number_of_Something': 212
      },
      1: {
        'Number_of_Something': 65
      },
      2: {
        'Number_of_Something': 657
      },
      3: {
        'Number_of_Something': 67
      }
    }
};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
Plain for..in
getMaxSomething(data);
pending…
Nested functions
getMaxSomething2(data)
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

Add a comment