checknested efficiency

JavaScript performance comparison

Revision 13 of this test case created by

Preparation code


      
      <script>
Benchmark.prototype.setup = function() {
  function checkNested(obj /*, level1, level2, ... levelN*/ ) {
    var args = Array.prototype.slice.call(arguments),
      obj = args.shift();
  
    for (var i = 0; i < args.length; i++) {
      if (!obj.hasOwnProperty(args[i])) {
        return false;
      }
      obj = obj[args[i]];
    }
    return true;
  }
  
  function checkNestedFast(obj /*, level1, level2, ... levelN*/ ) {
    for (var i = 1; i < arguments.length; i++) {
      if (!obj.hasOwnProperty(arguments[i])) {
        return false;
      }
      obj = obj[arguments[i]];
    }
    return true;
  }
  
  function checkNestedCatch(obj /*, level1, level2, ... levelN*/ ) {
    arguments = obj.split(".");
    obj = arguments[0];
    for (var i = 1; i < arguments.length; i++) {
      if (!obj.hasOwnProperty(arguments[i])) {
        return false;
      }
      obj = obj[arguments[i]];
    }
    return true;
  }
  
  var test = {
    level1: {
      level2: {
        level3: 'level3'
      }
    }
  };

};
</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
original
checkNested(test, 'level1', 'level2', 'level3'); // true
checkNested(test, 'level1', 'level2', 'foo'); // false
pending…
fast
checkNestedFast(test, 'level1', 'level2', 'level3'); // true
checkNestedFast(test, 'level1', 'level2', 'foo'); // false
pending…
try-catch
checkNestedCatch('test.level1.level2.level3'); // true
checkNestedCatch('test.level1.level2.foo'); // false
pending…

Compare results of other browsers

Revisions

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

0 Comments