1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

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

Discussão em 'Outras Linguagens' iniciado por Maaz ktk, Outubro 17, 2024 às 14:02.

  1. Maaz ktk

    Maaz ktk Guest

    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.
    }

    Continue reading...

Compartilhe esta Página