The game freezes and does not respond because of the nav

When I click on close points, the character moves, but when I click on far points, the game freezes and the program does not respond. Or I made a mistake while setting navigationRegion2D.
Screenshot_1

extends CharacterBody2D

@export var speed = 50
@onready var nav: NavigationAgent2D = $Navigation/NavigationAgent2D

var target = null
var homePos = Vector2.ZERO

func _ready():
homePos = global_position

nav.path_desired_distance = 4
nav.target_desired_distance = 4

func _physics_process(delta):
recalc_path()
if nav.is_navigation_finished():
homePos = global_position
return

var axis = to_local(nav.get_next_path_position()).normalized()
velocity = axis * speed
move_and_slide()

if axis.x < 0:
	$Sprite2D.flip_h = false
else:
	$Sprite2D.flip_h = true

func recalc_path():
if target:
nav.target_position = target
else:
nav.target_position = homePos

func _input(event):
if Input.is_action_just_pressed(“left_click”):
target = get_global_mouse_position()

The game freezes because you have too many polygons on the navigation map.

A far away point makes the pathfinding search far more polygons before it can exit the path search when it finds the target. That is why it still somehow works when the target is closer, it exits the path search quicker. If the target is not reachable the path search will need to search even far more polygons before it gives up.

You can see in the debug profiler tab and on the monitors the navigation polygon and edge count. If those numbers are very high performance will always turn into more and more of a problem. It is just too many polygons to search when you need to go from one corner of the map to the other. At that point the navigation mesh either needs to be more optimized with less detail and polygons, or a chunk system needs to be created for the game world to load only in what matters around the player.