How to fix heavy lag caused by pairs()

Hey there,
I’ve been working on a live screen renderer script (for testing, don’t plan on doing some banunos stuff), and I’ve come across an issue while looping through a table of colors.

Whenever I loop through the color list, there is a massive lag spike (a few actually!) due to using pairs. I’ve come across this as the issue while testing out what the main issue was.

Video:

Code:

local Current = Queue[1]
if Current and #Queue >= Threshold then
	local Pixels = Current.Pixels
	local List = Current.List
	
	local Published = List.Time
	local Frames = List.Frames
	
	local Delay = 1 / #Frames
	local Time = ostime()
	
	warn("Now rendering frames, received", Time - Published, "seconds ago")
	warn("Delaying by", Delay)
	
	for Frame, Colors in pairs(Frames) do
		warn("Rendering Frame", Frame)
		
		for XPos, ColorList in pairs(Colors) do
			local Found = Pixels[XPos]
			if Found then
				for Index, Part in pairs(Found) do
					local Color = ColorList[Index]
					if Color then
						local Converted = C3RGB(up(Color))
						Part.Color = Converted
					end
				end
			end
		end
		Wait:Wait(Delay)
	end
	
	remove(Queue, 1)

Is there any way to speed up pairs, or are there any alternatives I can use to remove the giant lag spikes (aside from decreasing the screen size)?

1 Like

Could add a wait() in the loop every few chunks (use the index as a way to monitor how many have loaded before adding a wait()) since whatever you’re executing is taking up the system resources

Adding a wait will not completely eliminate the lag, since I am still looking through a giant table of colors.

You’re saying the pairs is causing the lagg? Sounds like the loop is executing so much as once that it takes up whatever is allocated to roblox. So it won’t matter if you use a regular for loop?

I think the only way is to render in chunks (1 square section at a time) and find some balance to create as less lagg as possible.

or compress the colors (similar colors count as one color) so the table isnt as big

I have tried some compression methods although it was proven unhelpful. ipairs, pairs, and next will lag the loop no matter what. Even using a for loop to loop through the indexes of the color table will lag.
Rendering in chunks is kind of impossible due to it still lagging since pairs() is the actual main issue. Does this prove any efforts unusable?