Can a function execute over more than one tick to save performance?

Why not just do what we do in database world and make a queue.

You make a list of items to process, and when new stuff comes in you add it to the end of the list, and when you need to process the next thing, you take one off the top of the list.

That means your “queue” will need to be able to grow/shrink and/or be maanged, but it is the best practice way to process items the way you describe.

If said items are atomic (i.e. self contained) then this is also a good place to “multithread” as well.

The other suggestion of batching is also great if you simply want it to work ASAP.

The hardest part is getting to know:

How many items per tick (X)
How many items roughly in total (Y)

just to make sure Y / X == batches AND

Process all Batches time < total time you want to spend processing everything each time