At least 10 times a second, I have a function that is called. Each time, there are approximately 100 records with variations in LastSeenTime and ReadCount. In this simulation scenario, I understand the behavior; however, in real-time, the number of records in an array can range from 100 to 1000 and they may or may not be identical. It is essential for me to add all unique records to tagStore, which will then be displayed in the UI.
$scope.$on('getReadTags', function (event, tags) {
if (($scope.tagStore == null || $scope.tagStore.length == 0) && tags.length != 0) {
$scope.tagStore = tags;
}
else {
for (var i = 0; i < tags.length; i++) {
var notFound = true;
for (var j = 0; j < $scope.tagStore.length; j++) {
if (tags[i].TagID == $scope.tagStore[j].TagID) {
$scope.tagStore[j].ReadCount += tags[i].ReadCount;
$scope.tagStore[j].LastSeenTime = tags[i].LastSeenTime;
$scope.tagStore[j].DiscoveryTime = tags[i].DiscoveryTime;
notFound = false;
break;
}
}
if (!notFound) {
$scope.tagStore.push(tags[i]);
}
}
}
$scope.$apply();
});
Upon running this code, my browser becomes unresponsive, and I've noticed a significant spike in CPU and RAM usage. To address this issue, I require this method to only be invoked after the completion of another method.