Remove high performance cost feature
I consider myself a beginner. So I have one question for you.
I have this function
$scope.addLeagueToLines = function(league) {
league.active = !league.active;
if (league.active && !_.contains($scope.lineLeagues, league)) {
$scope.lineLeagues.push(league);
}else {
_.remove($scope.lineLeagues, function(lineLeague) {
return _.contains(lineLeague, league);
});
}
};
in the first conditional if
, everything is in order, it works correctly and pushes league
thin and smooth. The problem arises when I try to delete league
in part else
. Sometimes league
comes with a lot of data, lets say 200 items, so when you push a league this is not a problem. The problem is that when trying to delete those 200 items, the else
application slowly navigates to this part.
So what should I do to better remove elements in this function?
I work with lodash
, just in case.
Is there any other information you would like to know?
UPDATE
this is what the $scope.lineLeagues
console log returns
[
{
"id": "1517",
"composedId": "15170G",
"name": "NBA - Team Totals",
"lineType": "G",
"part": "0",
"offeringsAvailable": 2,
"sport": {
"id": 8
},
"active": true
},
{
"id": "5932",
"composedId": "59320G",
"name": "NBA",
"lineType": "G",
"part": "0",
"offeringsAvailable": 20,
"sport": {
"id": 8
},
"active": false
}
]
parameter league
{
"id": "1496",
"composedId": "14961I",
"name": "MLB (1I)",
"lineType": "I",
"part": "1",
"offeringsAvailable": 30,
"sport": {
"id": 6
},
"active": false
}
source to share
Do you really want
_.remove($scope.lineLeagues, function(lineLeague) {
return lineLeague == league;
});
I don't know why this one _.contains
got there. It uselessly enumerates all properties lineLeague
(treats the object as a collection) and tries to find league
as a value there.
You can also simplify it for
_.pull($scope.lineLeagues, league)
source to share
What are the elements of the array $scope.lineLeagues
? It looks like they should be objects league
.
If in this case I'm not sure about the logic in the branch else
.
Must not be:
leaguesToRemove = _.remove($scope.lineLeagues, function(lineLeague) {
return _.isEqual(lineLeague, league);
});
EDIT
After reviewing the update of the question, with the content $scope.lineLeagues
, I confirm that the problem is _.contains(lineLeague, league);
.
At this point lineLeague
is a JS object and you are checking if it contains another object.
What's really going on is that lodash will iterate over your array $scope.lineLeagues
, and then for each object, it will compare each property against the second argument you pass to _.contains()
: an object league
.
source to share