Which is more performant?

Option 1:

local function attachmentPreview()

	local mouseLocation = UserInputService:GetMouseLocation()
	local unitRay = camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
	local cast = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, CastParams)

	if cast and attachPreview then

		outline.Adornee = cast.Instance

		attachPreview.Position = toGrid(cast.Position)

		AttachmentArrows:PivotTo( CFrame.new(attachPreview.Position) )

		updatePreviewText(attachPreview.CFrame, cast.Instance.Name)
		
		return
		
	end
	
	PropertiesBarListPositionTextBoxes.XposButton.Text = ""
	PropertiesBarListPositionTextBoxes.YposButton.Text = ""
	PropertiesBarListPositionTextBoxes.ZposButton.Text = ""
		
	PropertiesBarListOrientationTextBoxes.XposButton.Text = ""
	PropertiesBarListOrientationTextBoxes.YposButton.Text = ""
	PropertiesBarListOrientationTextBoxes.ZposButton.Text = ""
		
	PropertiesBarListParentTextBoxes.XposButton.Text = ""
	
end

Option 2:

local function attachmentPreview()

	local mouseLocation = UserInputService:GetMouseLocation()
	local unitRay = camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
	local cast = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, CastParams)

	if cast and attachPreview then

		outline.Adornee = cast.Instance

		attachPreview.Position = toGrid(cast.Position)

		AttachmentArrows:PivotTo( CFrame.new(attachPreview.Position) )

		updatePreviewText(attachPreview.CFrame, cast.Instance.Name)
		
		return
		
	else
	
		PropertiesBarListPositionTextBoxes.XposButton.Text = ""
		PropertiesBarListPositionTextBoxes.YposButton.Text = ""
		PropertiesBarListPositionTextBoxes.ZposButton.Text = ""
		
		PropertiesBarListOrientationTextBoxes.XposButton.Text = ""
		PropertiesBarListOrientationTextBoxes.YposButton.Text = ""
		PropertiesBarListOrientationTextBoxes.ZposButton.Text = ""
		
		PropertiesBarListParentTextBoxes.XposButton.Text = ""

	end
	
end

Thanks!

1 Like

This function is bound to a Render Step by the way, like this:

RunService:BindToRenderStep("PreviewAttachment", Enum.RenderPriority.Camera.Value, attachmentPreview)
1 Like

I don’t see much differences in performance of the options.
But in Option 1, I see a more readable sense, since you don’t have to put this

	PropertiesBarListPositionTextBoxes.XposButton.Text = ""
	PropertiesBarListPositionTextBoxes.YposButton.Text = ""
	PropertiesBarListPositionTextBoxes.ZposButton.Text = ""
		
	PropertiesBarListOrientationTextBoxes.XposButton.Text = ""
	PropertiesBarListOrientationTextBoxes.YposButton.Text = ""
	PropertiesBarListOrientationTextBoxes.ZposButton.Text = ""
		
	PropertiesBarListParentTextBoxes.XposButton.Text = ""

in the else block.

-- This is maybe a representation

--[[Option1]]--
if condition then
    -- do something
    return
end
-- do something else

--[[Option2]]--
if condition then
    -- do something
else
    -- do something else
end

Yes, that’s the only difference. I’m wondering if it makes an impact on performance, since this function is running every render step. Thanks!

1 Like

Does this have to run on render stepped?

1 Like

yes, or else it’s really annoying if it’s tied to mouse.Move, feels very delayed

2 Likes

Option 1 would be quicker as the interpreter doesn’t have to check the else statement, it also reduces the size in memory of the abstract syntax tree.

Although it would be a negligible amount of difference between the two options, I would personally prioritize which one you can read better, in my opinion that’s option 1 again.

4 Likes
local function attachmentPreview()

	local mouseLocation = UserInputService:GetMouseLocation()
	local unitRay = camera:ViewportPointToRay(mouseLocation.X, mouseLocation.Y)
	local cast = workspace:Raycast(unitRay.Origin, unitRay.Direction * 1000, CastParams)

	if cast and attachPreview then

		outline.Adornee = cast.Instance

		attachPreview.Position = toGrid(cast.Position)

		AttachmentArrows:PivotTo( CFrame.new(attachPreview.Position) )

		updatePropertiesBarTextToSomething(attachPreview.CFrame, cast.Instance.Name)
		
		return
		
	end
		
	updatePropertiesBarTextToBlank()
	
	-- Would putting a "return" here be nessesary? Would it be faster? Thanks!
	
end

I would suggest NOT to add a return as it clutters up the code, unless you’d want to return false at the end and return true inside the if statement just to indicate that the method passed successfully.

Thank you so much :smiley:

30 chаracters

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.