Drupal has an API to process big tasks into small bits, and then processing one bit after another in an AJAX queue. This batch processing helps avoid PHP timeouts.