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)
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.
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
},
}