Scripting optimisation questions

Background - This part is not required to read through
I have been scripting a lot lately and… so far so good :grin: I absolutely enjoy scripting and for the past few weeks, I have come across that my scripting needs to be more optimised!! Hence this post. Part of my learning process is to observe and inspect other people’s work. I thrive off this method and I think it is the most effective method for me to expand my knowledge further. Obviously, everyone scripts differently and this is an issue for me. For example, if I were to look up a tutorial, there is a spectrum filled with scripters. And a very broad showcase of skills are shown but at the same time, I don’t necessarily know how I should write the scripts when I get back on Roblox studio. Questions like; Should I do this like him or write the script this way like her for my purposes. - Always pops up in my head.

Inquiry
After the brief introduction to the post, let’s move onto what I will inquire you about.
Below, I have a few questions; or rather a few lines of codes that I need to know which I should choose from in the future (Explain scenarios where I use this and that. If A/B is more optimised and should always be the option, just say that.). You don’t necessarily have to answer each one, just pick the one you are confident about answering in!

Lines of codes

  1. A)

for i in pairs() do
-- Code
end

  1. B)

for _, i in pairs() do
-- Code
end


  1. A)

local vectornew = Vector3.new
Part.Size = vectornew(1, 1, 1)

  1. B)

Part.Size = Vector3.new(1, 1, 1)


  1. A)

for _, v in pairs()

  1. B)

for _, v in next,


  1. A)

while true do
wait()
end

  1. B)

while wait() do
end


  1. A)

local Debounce = true
if Debounce == true then
-- Code
end

  1. B)

local Debounce = true
if Debounce ~= true then return end
-- Code


Remember to use motivation in why the alternative you picked is the better one!
Your contributions will be deeply appreciated - Thanks in advance :hugs:

1 Like

1: in (1a) and (1b) the variable i would stand for different things , in 1a, i would stand for the index portion of a table in the pairs loop, while in 1b, the i would stand for the value portion of that. if you are using an array here, and want to optimize for memory, i would suggest using a numeric for loop rather than a generic for, as that takes up slightly less memory.

2: 2b would take slightly less time than 2a, approximately 10 milliseconds if 2a and 2b were each ran 100000 times

3:I believe that using next would actually be slightly more optimal to use than pairs

4 i believe is more a matter of opinion

5: this is more a matter of opinion, but i would personally do neither, and do

local Debounce = true
if Debounce then
   ...
end

As if you do use a variable like that directly in an if statement, it will be run if its a truthy value, and not run if its a falsey value (false or nil)

1 Like

I’m not entirely sure of the other ones. However #2 did strike me as something I could possibly answer.

I would assume 2. A) would be less efficient compared to 2. B), as I assume 2. A) requires memory to hold your variable. However in any game you’ll be making on roblox, the performance will most likely be effected so little that it doesn’t matter and at this point would be off your preference.

If I’m wrong that 2. A) would use more memory compared to 2. B) please let me know though. We’re not perfect here. :slight_smile:

1 Like

All of this is negligible optimisation. Pick whatever is more readable to you. The only concern between each of these pieces of code is practice-related problems and readability. There are some things I will comment on though.

The first example means something completely different. The first code sample only traverses keys and ignores values, while the second one puts a placeholder variable to ignore keys and use only values. Using either one is dependent on the context of your code.

The second is a micro-optimisation of localising a global call to a local variable. I don’t do this at all, it looks horrible in my code. The difference it makes is negligible.

The third one means something different as well. Pairs calls next as well. You can read up on this behaviour in the Developer Hub if you’re interested in how pairs works.

The fourth one is the same thing but don’t use while wait() do. The first code sample of four uses while loops and their conditions properly, the second one abuses the fact that wait is a callback.

In the fifth one, those code samples are the same thing. The second one is just pointless and unnecessary. Only check for conditions that are passable or need code attached to them, don’t account for conditions that fail checks.

2 Likes

approximately 10 milliseconds if 2a and 2b were each ran 100000 times

Care to explain how you arrived at that 10ms figure?

running the code in a numeric for loop while using tick() to time it

Script 1:

local t = tick()

for i =1,100000 do
   workspace.Part.Size = Vector3.new(1, 1, 1)
end

print(tick()-t,"vnew function") -- 0.19797945022583 vnew function

Script 2:

local t = tick()

for i =1,100000 do
   local vectornew = Vector3.new
   workspace.Part.Size = vectornew(1, 1, 1)
end

print(tick()-t,"direct call") --0.18948841094971 direct cal

ik this isnt a perfect timing system, but with large enough numbers , the better the approximation gets

No, it doesn’t. These kinds of speed tests don’t really show anything major. The difference negligible. You realistically aren’t going to call a function 10,000 times in a loop with no interval.

The only difference between both is using the constructor from the datatype global and putting the constructor as a local variable to your thread.

1 Like

Empirical approaches like this to measure the efficiency of an algorithm have quite some implications (especially so here if it is the player running this on their machine vs Roblox’s servers), I think maybe a comparison with big O would more accurately answer OP’s question.