0

Is there any performance difference between:

Example 1:

$(window).on('resize', abc);

function abc(){
  //some abc code
}

Example 2:

$(window).on('resize', function(){
  //some abc code
});

If yes, what are the pros and cons of each?

If no, which is the preferred practice?

2
  • Performance difference in running that code once on page load, or performance difference for the event handling? As with your other question, unless you have code in a hot loop, execution speed differences are irrelevant. Commented Nov 3, 2016 at 5:57
  • First variant is slower, because it must first check function, than call it. But you will see mostly no difference. Commented Nov 3, 2016 at 5:59

1 Answer 1

1

Example 1:

$(window).on('resize', abc);

function abc(){
  //some abc code
}

In this abc() function can be used on window resize and we can use that function on any other event. But working of this function is slow as compared to Example 2

Example 2:

$(window).on('resize', function(){
  //some abc code
});

Example 2 works faster than Example 1. This function works only on Window resize.

If you want to use abc() function more than 1 Event then Example 1 is good otherwise Example 2 is good.

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

2 Comments

But working of this function is slow as compared to Example 2, Can you explain further?
@MathewJibin Yes. As it will first call function abc() and search for that function. So it is slower than Example 2

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.