QUERY:
Are there any discernible differences between the two instances of utilizing a factory service?
Instance 1:
angular.module('ramenBattleNetworkApp')
.controller('MainCtrl', function ($scope, Helpers) {
var testArray = [1,2,3,4];
// pass Helpers to var helpers
var helpers = Helpers;
helpers.arrayRotate(testArray, true);
});
Instance 2:
angular.module('ramenBattleNetworkApp')
.controller('MainCtrl', function ($scope, Helpers) {
var testArray = [1,2,3,4];
Helpers.arrayRotate(testArray, true);
});
factory:Helpers
angular.module('ramenBattleNetworkApp')
.factory('Helpers', function () {
var Helpers = {};
Helpers = {
history: [],
arrayRotate: function(arr, reverse){
Helpers.history.push(arr);
if(reverse)
arr.push(arr.shift());
else
arr.unshift(arr.pop());
return arr;
}
};
return Helpers;
});
});
Reason for inquiry: I have previously observed that invoking functions within a factory and transferring them among controllers can notably impact performance (instance 1), yet the reasons behind this remain unknown.
Although this particular scenario may seem contrived in comparison, I hypothesize that the factory generates a new object every time it is called. If multiple controllers were $watch()
for alterations on the factory's Helpers.history
and the factory was a substantial object; perhaps this explains the performance degradation. Moreover, saving it as a local variable in the controller could potentially offer better control over the process.
Alternatively, it is possible that my understanding is flawed.