Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upGitHub is where the world builds software
Millions of developers and companies build, ship, and maintain their software on GitHub — the largest and most advanced development platform in the world.
Performance issue in groupBy #189
Comments
|
I have the same issue |
|
We moved to a solution that works for our specific use case. It relies on the objects being grouped having an angular
.module( "util" )
.filter( "groupBy", groupByFilterProvider );
/* @ngInject */
function groupByFilterProvider( _ ) {
return function groupByFilter( array, groupKey ) {
if( !array || !array.length ) {
return array;
}
if( array.__groupByContainer ) {
var arrayId = idString( array, groupKey );
if( array.__groupById === arrayId ) {
return array.__groupByContainer;
}
}
array.__groupByContainer = {};
array.__groupById = idString( array, groupKey );
var elementProperty;
_.forEach( array, function addElementToGroup( element ) {
elementProperty = element[ groupKey ];
if( !array.__groupByContainer[ elementProperty ] ) {
array.__groupByContainer[ elementProperty ] = [];
}
array.__groupByContainer[ elementProperty ].push( element );
} );
return array.__groupByContainer;
};
function idString( array, key ) {
return _.reduce( array, function appendKey( id, element ) {
return id + element.id + element[ key ];
}, "" );
}
} |
|
+1 for this issue. groupBy works great for simple object arrays, but with complex objects it is really slow. The hashKey that's generated for a simple demo array of 62 complex objects is 61Kb. In the actual application, the array will be much larger. Update - I took a similar-ish approach to @oliversalzburg above. The hashKey I'm creating for the group objects is a concatenation of the object keys (as opposed to a JSON of everything like the actual library). So I call _groupBy with each iteration to get the keys, then pass them to a modified version of isMemoized that uses those keys instead of the giant one that it is currently using. Simply changing the keys has taken a 10 second plus render cycle to under 1 second...and calling _groupBy with each iteration only takes a millisecond or two. |
|
Hi @oliversalzburg ! On your code, what is the meaning of the underscore Edited: |
|
We usually wrap globals in Angular providers so that we can mock them. A simple way would be |
|
Is this fix going to be included in a new release anytime soon? |
|
Yes I have this issue as well, is there an upcoming release? |
|
+1 |
|
I changed my approach from my previous post. Now, I'm creating a unique key by using the grouping property concatenated with a random number. Then when the cache is checked, it looks for the non-random part of the key and checks to see if the collection is the same (the random part of the key is inferred from the collection match). So...
...And then in the filterWatcher, I changed and added...
...Checking the object pointers within the array (the isSame function) is much quicker than matching on massive object keys (at least in my case). |
|
I was also having heavy performance issue. Sad that this filter is so bad optimized.
|
I have an array of 300 non-trivial objects, which I want to filter with
groupBy. When I attempt to do that, my whole applications slows to a crawl.This is due to enormous amounts of time being spent in
getHashKey, whichJSON.stringifys the whole array to use that as the key for$$cache.If there's an alternative approach, I'd love to hear it :)