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

  • Home
  • SEARCH
  • 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 8012923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:29:06+00:00 2026-06-04T19:29:06+00:00

In JavaScript, what are specific reasons why creating functions within a loop can be

  • 0

In JavaScript, what are specific reasons why creating functions within a loop can be computationally wasteful?

On page 39 of JavaScript the Good Parts, Douglas Crockford states, “Avoid creating functions within a loop. It can be computationally wasteful”. I can’t seem to figure out why creating functions within a loop would be anymore wasteful than outside.

  • 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-04T19:29:07+00:00Added an answer on June 4, 2026 at 7:29 pm

    Because you’re creating several Function objects instead of reusing just one.

    Example of creating an identical function…

    for (var i = 0; i < 10; i++) {
        // v--creating identical function in each iteration.
        (function(j) {
            var el = document.createElement('a');
            el.onclick = function() { alert(j); };
            document.body.appendChild(el);
        })(i);
    }
    

    Example of reusing a named function…

    for (var i = 0; i < 10; i++) {
        var el = document.createElement('a');
          // ----------v---calling a reusable function
        el.onclick = createHandler(i);
        document.body.appendChild(el);
    }
    
    function createHandler(j) {
        return function() { alert(j); };
    }
    

    The two examples have the same outcome, but the second doesn’t require the overhead of making two functions during the loop. Instead it creates just the one handler.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How to run javascript/Jquery functions on user specific time scenario its so simple when
Let's assume I browse a specific web page that uses JavaScript to update its
Hey everyone I got a javascript problem I can't seem to find a specific
I am using Jquery to load a page's html and then select specific parts
In Rails, the common idiom for page-specific Javascript is: (layout) <head> <% yield :javascript
I want to call a javascript function within a protected scope and i can
I'm connecting to a webserver with a specific JavaScript. (Using HttpURLConnection atm) What i
I jave a Javascript code that draws a shape with specific color using style.backgroundColor=.
According to this website calling removeChild() in JavaScript causes an Internet Explorer Specific leak
I am using a charting javascript library that expects its data in a specific

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.