I don't think creating functions for all of your properties is a good idea. Not only will more function calls be made that will be executed every digest cycle to see if the value of the returned function has changed, but it really seems less readable and supported for me. It can add a lot of unnecessary code to your controllers and how to turn your controller into a viewing model. Your second case seems perfect, complex operations are similar to what you want your controller to handle.
As for performance, this makes a difference in accordance with the test I wrote (the script tried to use jsperf, but could not get different settings for each test). The results are almost twice as fast, i.e. 223,000 digests / sec using properties compared to 120,000 digests / sec using getter functions. The watch is designed for bindings that use angular $parse .
One thing to think about is inheritance. If you uncomment the ng-repeat list in the fiddle and inspect the scope of one of the elements, you can see what I'm talking about. Each created child region inherits the properties of the parent region. For objects, it inherits the link, so if you have 50 properties on your object, it only copies the reference value of the object to the area of ββthe child object. If you have 50 manually created functions, they will copy each of these functions to each child region to which it inherits. Timings are slower for both methods, 126,000 digests / sec for properties and 80,000 digests / sec with getter functions.
I really don't understand how it would be easier to maintain your code, and it seems to me that this is more difficult. If you don't want to touch your HTML if the server object changes, it would probably be better to do this in a javascript object instead of putting getter functions directly in your scope, i.e.:
$scope.obj = new MyObject(obj); // MyObject class
In addition, angular 2.0 will use Object.observe (), which should improve performance even more, but will not improve performance using getter functions in your area.
It looks like this code is executed for every function call. It calls contextGetter() , fnGetter() and ensureSafeFn() , as well as ensureSafeObject() for each argument for the scope itself and the return value.
return function $parseFunctionCall(scope, locals) { var context = contextGetter ? contextGetter(scope, locals) : scope; var fn = fnGetter(scope, locals, context) || noop; if (args) { var i = argsFn.length; while (i--) { args[i] = ensureSafeObject(argsFn[i](scope, locals), expressionText); } } ensureSafeObject(context, expressionText); ensureSafeFunction(fn, expressionText); // IE stupidity! (IE doesn't have apply for some native functions) var v = fn.apply ? fn.apply(context, args) : fn(args[0], args[1], args[2], args[3], args[4]); return ensureSafeObject(v, expressionText); };
},
In contrast, simple properties are compiled something like this:
(function(s,l /**/) { if(s == null) return undefined; s=((l&&l.hasOwnProperty("obj"))?l:s).obj; if(s == null) return undefined; s=s.subobj; if(s == null) return undefined; s=sA; return s; })