Hello I've been wondering if it's better to use Debris:AddItem() or task.delay()

So here is 2 code snippet that do the exact same thing, however I’d like to know which one is better to use and better optimized since I’ve seen in this dev forum thread there was a probability that debris will be deprecated : Debris MaxItems still in effect despite being deprecated

local sound = sfx:Clone() 

Debris:AddItem(sound, sound.TimeLength)

or

local sound = sfx:Clone() 

task.delay(sound.TimeLength, sound.Destroy, sound)
2 Likes

Wow. I so wasn’t aware of that and used Debris liberally, thinking it was the better way to do things. Thanks for alerting me to this.

Task.Delay is the more optimized and efficient way to use less memory and runs inside of it’s own task system- debris is unreliable and leads to memory leaks.

1 Like

Here are the results of the bench


It seems that @CremaCoffeeOverseer is correct and task.delay is significantly faster! I never knew.

And here is the code that i ran btw, in case you see any errors in it

Bench code
--[[
This file is for use by Benchmarker (https://boatbomber.itch.io/benchmarker)

|WARNING| THIS RUNS IN YOUR REAL ENVIRONMENT. |WARNING|
You may alter workspace, DataStores, etc. This is useful for testing
the performance of functions that interact with the game environment,
but be careful!
--]]

return {
	ParameterGenerator = function()
		local t = {}

		for i = 1, 1000 do
			table.insert(t,Instance.new("Part"))
		end
		
		return t, 2
	end,

	Functions = {
		["Debris:AddItem"] = function(Profiler, instances, duration)
			for _, v in instances do
				game.Debris:AddItem(v, duration)
			end
		end,

		["task.delay"] = function(Profiler, instances, duration) 
			for _, v in instances do
				task.delay(duration, v.Destroy, v)
			end
		end
	},
}
4 Likes

Holy, this is a perfect test. I love how you used a benchmark to test. Thank you very much, I’ll use task.delay instead of debris then by now

2 Likes

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