4

Is there any performance / memory hit differential among the three following styles?

Exhibit A:

var func = function() {
    // do some magic
}

$("#div").somePlugin({someEvent: func});

Exhibit B:

$("#div").somePlugin({someEvent: function() {
    // do some magic
});

Exhibit C:

function func() {
    // do some magic
}

$("#div").somePlugin({someEvent: func});
4
  • 2
    You should not drastically change the question once posted :) Commented Aug 17, 2010 at 18:05
  • "drastically" is a bit much, innit? Commented Aug 17, 2010 at 18:08
  • Yes, actually, the first vs the third has more implications and changes the cope of the question. Commented Aug 17, 2010 at 18:11
  • @Nick Well let's see an awesome response that blows minds! Commented Aug 17, 2010 at 18:18

2 Answers 2

3

There might be a little, slightly (really slightly) better performance for the function expression:

var func = function(){
};

That is a such called function expression. The otherside, the function statement is your third example:

function func(){
}

Function statements are converted internally into function expressions by ECMA-/Javascript, so thats the reason why it might(!) be slighty faster, but really, nothing to worry about.

Your B: example shows an anonymous function, which also has no performance impact over the A and C.

Sign up to request clarification or add additional context in comments.

Comments

0

There's no performance hit to speak of, it's more of a re-use/style thing, I'd say. In both A and C, the func becomes reusable, which can be helpful in certain situations. With B, you encapsulate functionality which is often desirable.

I prefer C as it's cleaner to read and enabled reuse without refactoring.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.