Skip to content

Code quality: Await is being used inside foreach loop #10268

@QuaintMako

Description

@QuaintMako

Description

Through the code, we can find some pieces of code looking like :

foreach (obj o in objectCollection)
{
    await AsyncMethod(o);
}

It is not the most efficient way of performing the tasks, since it means we are waiting for each task to be done before performing the next one. It would be much faster to perform all the task using the Task.WhenAll process.

As an exemple, using this process inside the AddAllItemsToSidebar method allowed for a gain of 15 % in speed.

Concerned code

  • Whole solution

Gains

  • A gain in performances, depending on how often the code is used.

Requirements

Replacing

foreach (obj o in objectCollection)
{
    await AsyncMethod(o);
}

By

var tasksCollections = objectCollection.Select(o=> AsyncMethod(o));
await Task.WhenAll(tasksCollections );

Comments

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions