Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Should I store "false" as boolean value table or will using "nil" be more efficient?

Asked by 4 years ago

Title. Right now, I have several true/false values that I evaluate from ModuleScripts. Unless I'm going to be iterating over these, it seems like storing "false" as a value wastes space as I could just evaluate

if bar then
    -- do stuff
end

instead of

if bar == true then
    -- do same stuff
end

Am I correct in this optimization?

1
wonderful question raid6n 2196 — 4y
1
wonderful question raid6n 2196 — 4y
1
wonderful question raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
View all comments (43 more)
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
1
i hope my answer helps raid6n 2196 — 4y
0
who upvoted all those comments ProbablyRiley 0 — 4y
0
who upvoted all those comments ProbablyRiley 0 — 4y
0
You shouldn't worry about very small optimizations unless you really need to. hiimgoodpack 2009 — 4y

2 answers

Log in to vote
0
Answered by
raid6n 2196 Moderation Voter Community Moderator
4 years ago
if bar then
    -- do stuff
end

would 100 percent work better

0
in all they would work the same but i use if blank then as it is easier and maybe more efficient raid6n 2196 — 4y
2
if isValid then is easier to understand than if isValid == true then royaltoe 5144 — 4y
Ad
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

They can essentially be the same thing.

For example, when checking a boolean value:

local bool = true

Both would check to see if bool == true:

if bool then
if bool == true then

By doing if bool then, you're basically telling the conditional statement to check for bool == true in this scenario. If you did this...

if not bool then

...then you telling the statement to check for not true, which is essentially bool == false. Efficiency is, therefore, almost entirely ignored when doing this.

When checking to see if a userdata exists, you can then feed the condition the object directly:

local inst = script.Parent:FindFirstChild("Humanoid") -- FindFirstChild() will return the Instance if it finds it, or nil if it can't find the Instance
if inst then

In this scenario, if inst then is equivalent to if inst ~= nil then, which can also be viewed as if (inst ~= nil) == true then since inst ~= nil will evaluate to either true or false.

"Efficiency" is not a relevant factor here. It's just simpler to do either one of these things:

if inst then
if not inst then
if bool then
if not bool then

As compared to:

if inst ~= nil or bool == true then
if inst == nil or bool == false then

Answer this question