Is there a performance difference between "task.defer" and "task.spawn"?

Sorry if it’s a basic question. I’m looping through 20 ‘profiles’ per second and they’re performing some heavyish calculations and for some reason when I don’t use task.spawn or task.defer, it seems to have false positives/break.

Anyways, is there a difference between the two in perfomance?

1 Like

There’s a slight difference. One executes the code immediately while the other defers to a later point in the current engine step.

  • If you want to queue up some code to run but don’t want it to stop your current code running then you should use defer.
  • If you want it to run immediately then you should use spawn.
  • If it doesn’t matter than you can use either, personally I default to defer.
3 Likes

Every function Roblox provides is well-optimized, so even though there might be a slight performance difference between the two functions, it won’t be noticeable.
The only different between them is that task.spawn() calls the function immediately through the scheduler, and task.def() calls it after the next resumption cycle.

1 Like

Small correction: defer calls it during the current (or next if one isn’t active) resumption cycle but it’s put to the back of the queue.

2 Likes

This should give you an idea of the performance difference

9 Likes

I feel like task.defer would be a better practice in terms of having a stable decent FPS; provides wider support on more devices, but I doubt there would be much noticeable difference. task.spawn makes your scripting experience more predictable