Thanks for reaching out to Q&A.
There could be various reasons for this random slowness/ delay in Function execution.
From the code, I can see that the document is read and set to response's context. So if the size of the input document is large, it is expected to take some time.
If we look at this issue from the Function platform perspective, you might want to check the diagnose and solve problems blade in the Function app portal and run the "Function app down and reporting errors" detector. Check the web app restarted detector to see if there were any restarts during the time the slowness was seen. Might be a coldstart if you are on Consumption plan or if you havent enabled "always on" on dedicated App service plan.
If the frequency of this issue is more. I would suggest you to add some logging in your code that would indicate which part of the code took longer time to execute. This would help in isolating the issue.
Please let me know the outcome.
I hope this helps!
Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.