What's faster? Bindable or Global Functions

Hello!
There’s no real issue to what I’m asking but it’ll help me to understand more clearly about the use of Global Functions and Bindable Events.
do not take game protection into the equation- more orientated towards performance

My question is what is fastest between Global Functions or Bindable Events. I have used Global Functions in some cases and they would do what I want to do. Bindable Events is an option to use but is there no real reason to use Bindables other Global Functions or vice versa.
Is it possible to exhaust Bindables?
What real uses can you make out for using Bindables over Global functions or in reverse.

1 Like

With BindableEvents you can connect multiple functions when it is fired, where global functions only fire one function. However, from what I know and what you’re asking, I’d say there’s no other reason to use one or the other. But, I’d say using ModuleScripts would be a lot more handy than global functions or bindable events to communicate. But, to answer your question, I don’t think there’s any major performance differences between the two.

3 Likes

One advantage of BindableEvents is that you can yield until they fire using the Event:Wait() method. Useful for some things such as character spawning or whatever you might want to wait for.
Also another thing to note when using :Fire() on a BindableEvent is that it ‘soft-yields’ (it waits until it has actually fired all of the events before the script continues - though this isn’t that important, but may factor in if you are making a lot of calls in a short timespan).

I don’t really use global functions - I use a ModuleScript with my functions in instead (pretty much the same result though). I fire events only for things I need to use :Wait() for, though.

Which is fastest though? I don’t know - and I don’t think it’s necessary to know unless you’re making some very intensive scripts. The difference will be negligible.

1 Like

If you want absolute performance, you should pass native Lua functions around and call them, such as the global functions you mentioned.

I know this because I had hundreds and sometimes thousands of connections to a BindableEvent in the lava manager script in ForbiddenJ’s Quarry, one for each lava block, and it was as laggy as a nightmare. When I switched to storing and calling the Lua functions directly, it was many times faster.

I would advise against putting things in the _G table, as that can get messy, and many other developers also advise against it. Use ModuleScripts instead.

1 Like

Technically you will be able to get a quicker invocation using globals. Bindables inherently mean that you have to go through an object first. In super-simple terms, there’s more CPU instructions necessary to invoke a bindable than a global function…I think. I don’t have proof of that, but I’m pretty confident in that.

I’m a bit confused by the comparison with bindable events though. Did you mean function?

Regardless, performance won’t be something that you will really have to worry about. I really don’t think you will have to worry about it, unless you’re trying to invoke this operation hundreds of times per frame. And I agree with @ForbiddenJ to use ModuleScripts instead of globals.

5 Likes

@Faultsified @Lightlimn @ForbiddenJ @Crazyman32 Thank you for giving me an insight !

@Crazyman32 I mean’t functions- yeah sorry wasn’t specific enough.

2 Likes

You’re correct.
Purely performance wise, BindableFunction/Events are likely a magnitude slower than simply calling a global function (which can even be localized if you prefer). This is only usually evident over many iterations, though.

3 Likes

I’m a bit confused by your question.
If you have a code such as this one:

local func = Instance.new("BindableFunction")
func.OnInvoke = function()
    print("hello")
end

you are still creating a normal function by doing = function().

A BindableFunction is just an instance which once invoked calls the global/local function you assigned to it. So yea it’s obviously going to be slower than calling the function directly.

1 Like

When I say Bindbales I mean BindableFunctions against Global functions (lua _G.function())
But thank you!