Angular $parse test (v2)

Revision 2 of this benchmark created on


Description

Testing $parse versus a javascript function to extract nested object using a string key. http://stackoverflow.com/questions/6491463/accessing-nested-javascript-objects-with-string-key

Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/angular.min.js">
</script>
<script>
function ParseCtrl ($scope, $parse) {
var data={
 depth: 1,
 nested: {
  depth: 2,
  nested: {
   depth: 3
  }
 }
};

$scope.parseData = function(i){
    $parse('depth');
    $parse('nested.depth');
    $parse('nested.nested.depth');
}

$scope.refData = function(){
   ref(data, 'depth');
   ref(data, 'nested.depth');
   ref(data, 'nested.nested.depth');
}

$scope.defensiveChecks = function(){
   data != null && 
   data.depth != null ? 
       data.depth : 
       undefined;

   data != null && 
   data.nested != null && 
   data.nested.depth != null ? 
     data.nested.depth : 
     undefined;

   data != null && 
   data.nested != null && 
   data.nested.depth != null && 
   data.nested.nested.depth != null ? 
     data.nested.nested.depth : 
     undefined;
}

function ref(obj, path, def){
        if (!path) return path;
        for(var i = 0, path = path.split('.'),len = path.length; i < len; i++){
            if(!obj || typeof obj !== 'object') return def;
            obj = obj[path[i]];
        }
        if(obj === undefined) return def;
        return obj;
    }
};
angular.element(document).ready(function() {
    window.ANGAPP = {
      scope: $('#angular').scope()
    };
  });
</script>
    <div style="width: 200px; height: 300px; overflow: hidden" ng-app>
    <span  ng-controller="ParseCtrl" id="angular"></span>
</div>

Test runner

Ready to run.

Testing in
TestOps/sec
Parse
ANGAPP.scope.parseData();
ready
Ref
ANGAPP.scope.refData();
ready
Defensive
ANGAPP.scope.defensiveChecks();
ready

Revisions

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