Back

PRF002

FindComponent / FieldByName in loop

Object Pascal — Delphi & Free Pascal
Warning
Performance

FindComponent and FieldByName perform linear searches; calling them inside loops is O(n*m)

FindComponent and FieldByName perform linear searches; calling them inside loops is O(n*m).

Why this severity

FindComponent and FieldByName perform linear searches; calling them inside loops is O(n*m).


Known false positives

loops with very few iterations where caching provides no benefit.

Remediation example

var
  NameField: TField;
begin
  NameField := Query.FieldByName('Name');
  while not Query.Eof do
  begin
    S := NameField.AsString;
    Query.Next;
  end;
end;

Reconnecting… Connection lost. Reload