Improve Resource performance

How can I improve the performance of this script without the text flickering
When I change Citizen.Wait(0) to Citizen.Wait(500) the performance becomes significantly better but the DrawText3D starts flickering. How do I fix that?

Citizen.CreateThread(function()
    while true do
        local playerPed = PlayerPedId()
        local playerCoords = GetEntityCoords(playerPed)
        local prop = GetClosestObjectOfType(playerCoords.x, playerCoords.y, playerCoords.z, 10.0, GetHashKey("prop_med_stretcher"), false, false, false)
        local propCoords = GetEntityCoords(prop)
		
		local distance = Vdist(playerCoords.x, playerCoords.y, playerCoords.z, propCoords.x, propCoords.y, propCoords.z)

        if(distance < 2) then
            DrawText3D(propCoords.x, propCoords.y, propCoords.z + 0.5, "[E] Hinlegen")
			if IsControlJustReleased(0, 38) then -- E
				ExecuteCommand("getintostr")
			end
        end
        
        Citizen.Wait(0)
    end
end)

function DrawText3D(x,y,z, text)
    local onScreen,_x,_y=World3dToScreen2d(x,y,z)
    local px,py,pz=table.unpack(GetGameplayCamCoords())
    
    SetTextScale(0.35, 0.35)
    SetTextFont(4)
    SetTextProportional(1)
    SetTextColour(255, 255, 255, 215)
    SetTextEntry("STRING")
    SetTextCentre(1)
    AddTextComponentString(text)
    DrawText(_x,_y)
    local factor = (string.len(text)) / 370
    DrawRect(_x,_y+0.0125, 0.015+ factor, 0.03, 0, 0, 0, 80)
end

Make a separate thread for the drawtext. And then in the original thread if dist < 2 then drawtext =true else drawtext = false. Then have the drawtext thread wait 0 while the original thread can be 100-200 or something.

As ChieF-TroN mentioned; two threads wouldn’t be too bad.

Something like this maybe. Not tested.

function DrawText3D(x,y,z, text)
    local onScreen,_x,_y=World3dToScreen2d(x,y,z)
    local px,py,pz=table.unpack(GetGameplayCamCoords())
    
    SetTextScale(0.35, 0.35)
    SetTextFont(4)
    SetTextProportional(1)
    SetTextColour(255, 255, 255, 215)
    SetTextEntry("STRING")
    SetTextCentre(1)
    AddTextComponentString(text)
    DrawText(_x,_y)
    local factor = (string.len(text)) / 370
    DrawRect(_x,_y+0.0125, 0.015+ factor, 0.03, 0, 0, 0, 80)
end

local isNear = false
local propCoords = nil

CreateThread(function()
	while true do
		local playerPed = PlayerPedId()
        local playerCoords = GetEntityCoords(playerPed)
		local prop = GetClosestObjectOfType(playerCoords.x, playerCoords.y, playerCoords.z, 10.0, GetHashKey("prop_med_stretcher"), false, false, false)
        propCoords = GetEntityCoords(prop)
		if propCoords then
			local distance = #(playerCoords-propCoords)
			if distance < 2.0 then
				if not isNear then
					isNear = true
				end
			else
				if isNear then
					isNear = false
                    propCoords = nil
				end
			end
		end
		Wait(1000)
	end
end)

CreateThread(function()
	while true do
		Wait(1)
		if isNear then
			DrawText3D(propCoords.x, propCoords.y, propCoords.z + 0.5, "[E] Hinlegen")
			if IsControlJustReleased(0, 38) then -- E
				ExecuteCommand("getintostr")
			end
		else
			Wait(500)
		end
	end
end)

This works well, thank you very much.
But how can I make the

disappear after the player pressed E?

Something like this maybe.

function DrawText3D(x,y,z, text)
    local onScreen,_x,_y=World3dToScreen2d(x,y,z)
    local px,py,pz=table.unpack(GetGameplayCamCoords())
    
    SetTextScale(0.35, 0.35)
    SetTextFont(4)
    SetTextProportional(1)
    SetTextColour(255, 255, 255, 215)
    SetTextEntry("STRING")
    SetTextCentre(1)
    AddTextComponentString(text)
    DrawText(_x,_y)
    local factor = (string.len(text)) / 370
    DrawRect(_x,_y+0.0125, 0.015+ factor, 0.03, 0, 0, 0, 80)
end

local isNear = false
local hasPressed = false
local propCoords = nil

CreateThread(function()
	while true do
		local playerPed = PlayerPedId()
        local playerCoords = GetEntityCoords(playerPed)
		local prop = GetClosestObjectOfType(playerCoords.x, playerCoords.y, playerCoords.z, 10.0, GetHashKey("prop_med_stretcher"), false, false, false)
        propCoords = GetEntityCoords(prop)
		if propCoords then
			local distance = #(playerCoords-propCoords)
			if distance < 2.0 then
				if not isNear then
					isNear = true
				end
			else
				if isNear then
					isNear = false
                    propCoords = nil
                    hasPressed = false
				end
			end
		end
		Wait(1000)
	end
end)

CreateThread(function()
	while true do
		Wait(1)
		if isNear and not hasPressed then
			DrawText3D(propCoords.x, propCoords.y, propCoords.z + 0.5, "[E] Hinlegen")
			if IsControlJustReleased(0, 38) then -- E
                hasPressed = true
				ExecuteCommand("getintostr")
			end
		else
			Wait(500)
		end
	end
end)