I was looking at default Animate local script that creates whenever character has been created. Then I found the line that declares animator by the method below.
local animator = if Humanoid then Humanoid:FindFirstChildOfClass("Animator") else nil
Why does it use this method instead of using
local animator = Humanoid and Humanoid:FindFirstChildOfClass("Animator")
like this? And does it perform better than using “and” or “or” operator?
Because the first code will make sure that the Humanoid first before defining Animator so it won’t error if it somehow doesn’t detect Humanoid, and will only return nil.
local animator = Humanoid and Humanoid:FindFirstChildOfClass("Animator")
Isn’t this invalid since you’re using and which makes it look like you’re trying to define 2 items in one variable at the same time?
Logical operator “and” evaluates the second operand when the first operand resolves to be true. “or” is the opposite. It appears they both work as same but I wonder if there’s performance difference between them.
The only difference between them is that the original code will check if Humanoid exists before defining, for the second one it directly defines Animator and nothing else. I might be wrong so feel free to correct me
I’m pretty sure these have negligible performance differences, just use whichever you prefer to read. Using an if statement to assign a variable is newer, so most code uses the other notation.
The results of those two expressions are equivalent, but they are logically not exactly the same.
and/or is an emulation of ternary operators, but here is one case when they don’t behave like ternaries.
if-then-else is endorsed by Roblox in Luau because it is a complete first-class ternary implementation, and because it promotes code clarity by always requiring an explicit else.
Differences:
print(true and true or true) --> true
print(true and false or true) --> true
print(if true then true else true) --> true
print(if true then false else true) --> false
Performance difference?
They both bring some performance advantages over working with separate blocks in if-statements, but like others said, we can’t speak of any performance differences between the two.
Wanted to add that there was an engine update who’s changelog mentioned performance improvements for ternary operators, but it doesn’t say anything about how it compares with the and-or idiom
Good to know! I was thinking about benchmarking, though in all probability we can’t take completely accurate and realistic measurements, which goes to show that choosing one over the other based on performance is even beyond micro-optimisation.
Still, that didn't deter me from opening the studio and verifying that they have the same execution time from our point of view.
local rand = Random.new()
local t = true
local f = false
local conditions = {}
for i = 1, 10 do
conditions[i] = {}
for j = 1, 1e7 do
table.insert(conditions[i], if rand:NextInteger(0, 1) == 1 then t else f)
end
task.wait()
end
task.wait(6)
local c = os.clock()
for _, array in conditions do
for _, v in array do
local x = if v then "First" else "Second"
end
end
print(`if-then-else {os.clock() - c}`)
task.wait()
local c = os.clock()
for _, array in conditions do
for _, v in array do
local x = v and "First" or "Second"
end
end
print(`and/or {os.clock() - c}`)