How to test for equality of functions in Javascript [duplicate]

How would I get a positive test for bar and foo's equality?

 foo = function() {
    a = 1;

 bar = function() {
    a = 1;

 if (foo === bar) alert('baz');
 if (foo == bar) alert('qux');

Both the above conditionals are false.

Updated - As requested the reason I need to test for function equality

I am building a Publish/Subscribe framework and need to pass the callback inorder to unsubscribe to a topic.

Please see the fiddle:

You could check whether the content of two functions is exactly the same by comparing the result of calling toString on them

 var foo = function() {
    a = 1;

 var bar = function() {
    a = 1;

alert(foo.toString() == bar.toString());?

That will, however, fail if there is but one character that is different. Checking to see if two functions do the same thing, on the other hand, is pretty much impossible.

The problem is that there're different notions of equality on functions.

  • Reference equality. Functions are stored somewhere in memory, and start from a certain address. For two function names (which are essentially addresses inside) reference equality gives you true iff both names point to the same address. It's a matter when you assign one bar = foo. Reference equality is used in JavaScript.
  • Equality on source code as proposed in another answer. It won't work, if you change any character in function body. Also, toSource is currently (as of 2014) only available in Firefox, as it's not W3C standartized.
  • Syntactic equalities on source code. One could use abstract syntax trees to resist against modifications in spaces and comments. One could use alpha-equivalence (i.e. equivalence of functions, where variables are consistently renamed) as defined in lambda calculus, but also there is
  • Extensional equality, when functions are equal if they work the same way. There's a theorem stating that there's just no algorithm to check for extensional equality of functions, so it's certainly not the one you're searching for. Though, in some more advanced languages than JS there are
  • Intensional equality (where you either prove that functions are equal by hand or your program doesn't compile) and
  • Observational equality (this one is too advanced to explain).

So, take care of what you think equality is.

If you look at the Node.js source for events

you can see that the core team uses === to compare functions for equality.

I am not sure how === is implemented for functions, but I sure hope it's not toString(), since two different functions might have the same toString() result.

You can test to see whether two variables refer to the exact same function object, and you can convert functions to strings and see if they're really exactly the same, but trying to determine whether two functions do the exact same thing would be a little harder, or maybe a lot harder.

Running a pair of functions through a minifier would be interesting, because minifiers do a considerable amount of static analysis.

It seems like if you need this functionality, it might be better to restructure your program a bit. Something like this could work:

function baz(myInt) { return 3 + myInt; }
function asd(a) { return 3 + a; }

var foo = baz;
foo(1); //returns 4

var bar = baz;
bar(3); //returns 6

foo === bar; //true

bar = asd;
bar(3); //returns 6

foo === bar; //false

Basically, it's impossible in general. There is no way to test functional equality in javascript. The closest you can get is either compare their code as string for equality.

Simple approach: If you implement your subscribe API to return a unique ID every time it is invoked, then it will be as simple as invoking unsubscribe API with that ID. Internally in simple scenario you would use a simple array to store all subscribers but with this approach you will have to store. An ID can be a simple counter that you can increment with every single subscribe call.

{ID: subscriber' callback function}

So iterate over all subscribers when you want to publish to all subscribers.

Advanced approach: Instead of accepting a simple function name in subscribe API, accept an object of a class which should have an onMessage function (or whatever name you want to give this method). Then you can simply pass same instance of object to unsubscribe API to get yourself unregistered. When you need to notify subscriber you can invoke instance.onMessage function of all subscribers. Comparing two object reference is easy.