nuwave / lighthouse

A framework for serving GraphQL from Laravel
https://lighthouse-php.com
MIT License
3.37k stars 439 forks source link

Update batch reference resolvers example code snippet #2595

Closed kfc closed 3 months ago

kfc commented 3 months ago

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 return NULL by default.

spawnia commented 3 months ago

Thank you, good suggestion