Capella There isn't much difference in performance, I just benchmarked it. I made these three functions that return the names of the nodes:
@onready var label = $VBoxContainer/HBoxContainer2/VBoxContainer/BestResultClassLabel
func return_onready() -> String:
return label.name
func return_shorthand() -> String:
return $VBoxContainer/HBoxContainer2/VBoxContainer/BestResultClassLabel.name
func return_get_node() -> String:
return get_node("VBoxContainer/HBoxContainer2/VBoxContainer/BestResultClassLabel").name
Running those calls 50k times gives:
@onready
takes 20470 microseconds.
- The
$Foo
shorthand takes 26521 microseconds, which makes it 29.5% slower.
get_node()
takes 27217 microseconds, which is 33% slower.
Not surprising, really. $Foo
is just shorthand for get_node("Foo")
and if I had to guess it's just slightly faster because under the hood the compiler eliminates one function call and/or passing the string argument. I don't know, but it's just a tiny bit faster.
Using an @onready
variable is of course the fastest because you do the node lookup only once, when your script is ready, and not each function call.
So, if you can and need to access a certain node multiple times then store it in a variable.