How can I optimize my pathfinding script?

I have a pathfinding script but the problem is I’m worried calling a lot of Path:ComputeAsync()s per second will lag a lot. I’m also trying to make an npc smoothly pathfind to a player with as least amount of lag. The problem is, it seems to call a lot of Path:ComputeAsync()s per second and it seems to start lagging a lot in terms of movement when you get close to the npc and move farther away. Here is the script:

local Player = game.Players.PlayerAdded:Wait() or game.Players:GetChildren()[1]
Player.CharacterAdded:Wait()
local PFS = game:GetService('PathfindingService')
local RunService = game:GetService('RunService')
local Path = PFS:CreatePath()
Path:ComputeAsync(script.Parent.Parent.HumanoidRootPart.Position,Player.Character.HumanoidRootPart.Position)
local PathFinished = true

while true do
	if PathFinished and Path.Status == Enum.PathStatus.Success then
		local Waypoints = Path:GetWaypoints()
		local StartPos = Player.Character.HumanoidRootPart.Position
		
		for i,v in pairs(Waypoints) do
			coroutine.wrap(function()
				PathFinished = false
				Path:ComputeAsync(v.Position,Player.Character.HumanoidRootPart.Position)
				PathFinished = true
			end)()
			script.Parent:MoveTo(v.Position)
			script.Parent.MoveToFinished:Wait(2)
			if i > 1 and (Player.Character.HumanoidRootPart.Position - StartPos).Magnitude > 1 then
				print('Broke!')
				break
			end
		end
	else
		print('aaaaaaaaaaaa')
		
		if PathFinished then
			Path:ComputeAsync(script.Parent.Parent.HumanoidRootPart.Position,Player.Character.HumanoidRootPart.Position)
		end
		
		RunService.Heartbeat:Wait()
	end
end

Well, I am not familiar too much with pathfinding myself,
but as far as I understood so far you don’t have to use pathfinding when the player is right in front of you, in that situation you can just use MoveTo()

so you’ll have to raycast and check if the player is visible to the npc, if he was, then you can just use MoveTo(), else you’ll have to use pathfinding!

Also I would suggest you to check out this module which is very helpful

EZ Pathfinding V3 - Resources / Community Resources - DevForum | Roblox