Really fast way to check if something is a BasePart

I have a VERY tight loop that runs on the entire Roblox world every RenderStepped. It’s seriously tight and it’s lagging the game. I need it to be faster.

One of the bottlenecks are IsA checks to see which things are BaseParts. So I need a really fast way to check if something is a BasePart. Faster than IsA. My loop takes 2 or 3 milliseconds with no code in it except for an IsA check.

Any ideas? I’m going to try adding an extra lookup table to store BaseParts if I can’t make polling faster.

1 Like

Uh… why? From what I understand, RenderStepped is meant to be used for things like the camera.

EDIT: If you paste your code, we can try to help you optimize it / find a better way to do what you’re trying to accomplish.

2 Likes

Are you dealing with general regions in space, like using a Region3? Or is this just under some specific objects that you are checking?

It is a camera, in a certain sense. It’s actually a clone of workspace that I’m trying to keep in sync with the real one.

The entirety of workspace, every frame.


Got any ideas for faster IsA check?

You might be able to use tags on the parts you are looking for. Just tag each basepart with a certain tag you are looking for and that should be faster I think.

All the details you need should be in the post. Take a look at this; this is basically it.

	for i, part in pairs(workspace:GetDescendants()) do
		if part:IsA('BasePart') then
			-- ... 
			-- This if statement was empty for my benchmark.
		end
	end

I’m looking for every BasePart in workspace. Indiscriminately.

It’s a bit more complex than that because I’m not actually looping through workspace’s descendants (else a lookup table would be lots faster), but rather a lookup table mapping workspace instances to clone instances. There are more than BaseParts in there so that Humanoids display correctly and etc.

I’m essentially displaying Workspace in a viewport frame.

But can you not tag every basepart? I’ve never used tags before but I imagine that it should be possible to do that. Even if you instance new baseparts, I’m pretty sure you can still tag them.

If it’s lagging I’d assume it’s because you’re running an iterator with such a costly method :GetDescendants() 60 times per second…I don’t think optimizing how you determine if a part is a BasePart would help anything, unless I misunderstood your original post.

2 Likes

No, I’m not. I’m using pairs with a lookup table already populated with instance → clone pairs. I’m not actually running GetDescendants.

If you want my code so badly then here you go. I tried to give you all the information you needed.

	for part, clone in pairs(self.WorldLookupA) do
		if part:IsA('BasePart') then
			-- nothing for the benchmark
		end
	end

The loop takes 2ms with a single function call in it :stuck_out_tongue:

Optimizing the function call is my goal.

Why not use ipairs which is faster?

Why not store the returned value of :IsA once in a dictionary with the key being the part in the current iteration? That way you only have to call it once.

Instance pairs? LMAO

No, ipairs only loops through numerical keys.

I ended up using a lookup table to store all BaseParts so I don’t have to do any checking. The BasePart lookup table gets populated when the clones are created, and only once.

Thank you all for your input.

my bad i didn’t realize you weren’t using :GetDescendants() directly

2 Likes