The suggested code snippet for batch reference resolvers uses the firstWhere method on Collection of results. This means that for every representation it searches the value with the ID in the collection (O(n) complexity). It's not a problem on small amounts of entities, however when a number of representations is quite large, it becomes slower and slower. In the project I was using it, it was 2x slower (40s vs 20s)
With the suggested change the collection is mapped by key (ID) and then uses simple array access by key to find the value (O(1) complexity), or if not found, get method will return NULL by default.
Changes
The suggested code snippet for batch reference resolvers uses the
firstWhere
method on Collection of results. This means that for every representation it searches the value with the ID in the collection (O(n) complexity). It's not a problem on small amounts of entities, however when a number of representations is quite large, it becomes slower and slower. In the project I was using it, it was 2x slower (40s vs 20s)With the suggested change the collection is mapped by key (ID) and then uses simple array access by key to find the value (O(1) complexity), or if not found,
get
method will returnNULL
by default.