Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9059809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T15:03:59+00:00 2026-06-16T15:03:59+00:00

What would be more efficient in terms of computer resources. Putting the event handler

  • 0

What would be more efficient in terms of computer resources. Putting the event handler inside of a loop like this:

$('ul li').each(function()
{
    $(this).mouseover(function()
    {
        // code to do something 
    });

Or having the function outside of the loop and creating a call to it inside the loop like this:

$('ul li').each(function()
{
    $(this).mouseover(function()
    {
        doStuff($(this)); 
    });

function doStuff(liElem)
{
    // code to do something 
}

It seems like to me that the second option would be easier on the computer because the code to do something wouldn’t be repeated each time the loop iterates. Does the code of the event handler get created in the computer’s memory every time through the loop, or is it just created once? Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T15:04:00+00:00Added an answer on June 16, 2026 at 3:04 pm

    There can be various optimizations possible but keeping it specific to the approach you have asked for,
    please find the answer as inline comments in the code below

    First approach:

    $('ul li').each(function()
    {
        // Maybe you might like to declare some variables here
        $(this).mouseover(function()
        {
            // code to do something
    
            // Maybe you might like to use the variables declared in above function
    
            // Disadvantage over other approach
            // The code written here will need to store the context information of the outer function and global context
    
            // Advantage over other approach
            // You can directly access the variables declared in the above function
        });
    }
    

    Or having the function outside of the loop and creating a call to it inside the loop like this:

    Second approach:

    $('ul li').each(function()
    {
        // Maybe you might like to declare some variables here
        $(this).mouseover(function()
        {
            doStuff($(this)); 
        });
    });
    
    function doStuff(liElem)
    {
        // code to do something
    
        // Advantage over other approach
        // The code written here will need to store the context information only for the global context
    
        // Disadvantage over other approach
        // You cannot directly access the variables declared in the outer function as you can in the other approach,
        // you will need to pass them to the doStuff function to access here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm curious as to which would be more cost efficient in terms of resources
Which of these queries is more efficient, and would a modern DBMS (like SQL
I edited the question so it would make more sense. I have a function
I would like to know more about how the thread sleep resolution works, and
I would like to have more than one button. I tried to copy code
I would like to have a more colorful Python prompt in the terminal, just
I have a concatenated string like this: my_str = 'str1;str2;str3;' and I would like
Which one of these would be more suitable for RLE on bytes? I need
I have a lot of front-end code that would be more readable if I
Sometimes when I'm writing XLANG/s code in an expression shape, it would be more

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.