2

I have two objects:

$scope.copy;
$scope.myObject;

Now when ever user changes anything it is reflected in myObject variable.

Later on I would like to compare two scope variables for equality like below:

angular.equals($scope.copy,$scope.myObject);

But the problem here is whenever user updates anything Angular.JS add hashkey and so because of that my above comparison is failing.

So as per my research, I found that I need to add track by everywhere to remove $hashkey and adding track by boost up performance as per following references:

https://www.timcosta.io/angular-js-object-comparisons/

What is the $$hashKey added to my JSON.stringify result

But I have some confusion and I hope if someone could clear it:

  1. Does Angular add a hashkey in JSON even if I update any textbox value, or it add only in case of ngrepeat?

  2. With ngrepeat can I blindly add track by $index or I need to add unique field like track by id but in some case I don't know? Would value will be unique or duplicate so can I add everywhere track by $index?

  3. If I blindly add track by $index instead of track by id (i.e name of field) then will I have same performance benefit without any problem as we have in track by id?

halfer
  • 19,824
  • 17
  • 99
  • 186
I Love Stackoverflow
  • 6,738
  • 20
  • 97
  • 216

0 Answers0