October 22, 2024
Chicago 12, Melborne City, USA
PHP

Merging Two Laravel Collections with Same Keys for Aggregation has performance issue


I’m currently working on a Laravel application that merges statistics from two collections: postStats (a standard Collection) and contentStats (a LazyCollection). The mergeStats method I’m using iterates through each collection multiple times to sum various metrics, which is causing performance issues when dealing with large datasets, For 1000 posts and 1000 contents, it takes around 10 seconds, The records can be much greater than this.

Code:

private function mergeStats(Collection $postStats, LazyCollection $contentStats, bool $dashboardStats = false): array
{
    $startTime = microtime(true);
    $keys = [
        'reactions_count',
        'comments_count',
        'sc_shares_count',
        'confirmed_regular_shares_count',
        'sc_clicks_count',
        'unconfirmed_regular_shares_count',
        'confirmed_shares_click_count',
    ];

    // Current logic here to merge postStats and contentStats, 
    $mergedStats = $postStats->concat($contentStats)->reduce(static function ($carry, $stats) use ($keys) {
        foreach ($keys as $key) {
            $carry[$key] = ($carry[$key] ?? 0) + ($stats[$key] ?? 0);
        }
        return $carry;
    }, array_fill_keys($keys, 0));

    $mergedStats['dashboard_stats'] = $dashboardStats;

    return CalculateEngagementMetricsAction::run($mergedStats); //based on the merged stats, it does some calculations, This action is not the culprit for performance. 
}



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video