Skip to content

Refactor for-loop iterations to use numeric indexing (Micro-Optimization)#271

Open
Vitroze wants to merge 4 commits intoStyledStrike:mainfrom
Vitroze:ipairs
Open

Refactor for-loop iterations to use numeric indexing (Micro-Optimization)#271
Vitroze wants to merge 4 commits intoStyledStrike:mainfrom
Vitroze:ipairs

Conversation

@Vitroze
Copy link
Copy Markdown
Contributor

@Vitroze Vitroze commented Apr 9, 2026

Hello,
This PR aims to add a color cache and replace all ipairs statements with a for i = 1 loop.

I haven't tested it with other glide-compatible addons, but this one should work.

Sorry for StyledStrike.
image

- Updated multiple files to replace `ipairs` with numeric indexing for better performance and consistency.
- This change affects various entities and client/server scripts, ensuring that iteration over arrays is done using a standard numeric approach.
- The changes enhance readability and maintainability of the codebase.
@Vitroze
Copy link
Copy Markdown
Contributor Author

Vitroze commented Apr 9, 2026

I'll sort out the style issues this evening.

@Astralcircle
Copy link
Copy Markdown
Contributor

I don't think it will speed up anything, LuaJIT handles this just fine

@Vitroze
Copy link
Copy Markdown
Contributor Author

Vitroze commented Apr 9, 2026

I don't think it will speed up anything, LuaJIT handles this just fine

However, according to another developer's benchmark, this changes.

Performance test over 100,000,000 iterations...
Digital loop: 0.5139 seconds
ipairs loop: 1.5544 seconds
10^1        | 0.00000001 s | 0.00000001 s | +8.14%
10^2        | 0.00000001 s | 0.00000001 s | +15.38%
10^3        | 0.00000523 s | 0.00001517 s | +190.13%
10^4        | 0.00005160 s | 0.00015319 s | +196.88%
10^5        | 0.00052010 s | 0.00152060 s | +192.37%
10^6        | 0.00513500 s | 0.01523100 s | +196.61%
10^7        | 0.05127500 s | 0.15289300 s | +198.18%
10^8        | 0.51848100 s | 1.54321600 s | +197.64%

In fact, this is interesting when the function is called several thousand times.

@bloodycop6385
Copy link
Copy Markdown

for i is quite faster lol

@Astralcircle
Copy link
Copy Markdown
Contributor

for loop is faster only if you initially know the length of the table, and nothing will iterate through the table ~100 million times

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants