CleanArchitecture icon indicating copy to clipboard operation
CleanArchitecture copied to clipboard

Optimize improve performance by using AnyAsync

Open tyfndmrl opened this issue 1 year ago • 0 comments

  • Replaced the AllAsync method with AnyAsync in the BeUniqueTitle method of CreateTodoListCommandValidator.
  • This change improves performance by stopping the query as soon as the first matching title is found, rather than checking all records.
  • The new implementation checks if any TodoList record has a matching title, which is more efficient, especially for large datasets.

Changes:

  • Before:
    return await _context.TodoLists
        .AllAsync(l => l.Title != title, cancellationToken);
    
  • After:
    return !await _context.TodoLists
        .AnyAsync(l => l.Title == title, cancellationToken);
    

This optimization enhances performance when validating unique Title values.

tyfndmrl avatar Oct 12 '24 17:10 tyfndmrl