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 6129075
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:42:01+00:00 2026-05-23T16:42:01+00:00

At some point in the past, I read something that gave me the idea

  • 0

At some point in the past, I read something that gave me the idea that anonymous functions in JavaScript can use up a surprising amount of memory (because they carry the entire current scope around with them), whereas named (static?) functions don’t have this issue.

I can’t remember where I read this, so I can’t go back and re-read it and figure this out for myself.

I’ve got two questions:

  1. Are there situations where anonymous functions can use enough memory for it to be worth caring about? (If so, do you have an example?)
  2. Are there any other drawbacks to anonymous functions (as opposed to named/static functions)?
  • 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-05-23T16:42:01+00:00Added an answer on May 23, 2026 at 4:42 pm

    All JavaScript functions will behave in the same manner in that they inherit the variable environments in entire scope chain leading up to, and including, themselves. This is equally true for both anonymous and named functions.

    This chain of references to the outer environments stays with each function, even if the function is passed into an entirely different scope.

    Traditionally, this would mean that all variables in any given chain have a reference retained to them as long as the inner closure continues to exist. Although in modern browsers that compile the code, it is likely that there will be an analysis of which variables are actually referenced, and only those will be retained, allowing others that are no longer referenced to be garbage collected.

    However, there are other situations where an anonymous function is wasteful.

    Here’s a common bit of code:

    for( var i = 0; i < 100; i++ ) {
        (function( j ) {
            setTimeout( function() { console.log( j ); }, 1000 );
        })( i );
    }
    

    This is a situation where an anonymous function is a bit more wasteful than a named function because you’re recreating an identical function 100 times during the loop when you could just reuse a named one.

    function setConsole( j ) {
        setTimeout( function() { console.log( j ); }, 1000 );
    }
    
    for( var i = 0; i < 100; i++ ) {
        setConsole( i );
    }
    

    This has the exact same closure overhead, but is more efficient because you’ve only constructed one function to create each new variable environment.

    http://jsperf.com/immediate-vs-named (Thanks to @Felix Kling for the jsPerf.)

    So with respect to the closure in particular, yes there’s overhead as long as you maintain the closure by some permanent reference. I’d say that it is good to avoid this if possible but not to be obsessive about it. Sometimes a new variable environment added to the scope chain is simply the best solution.


    EDIT: Here’s an article from Google. Specifically, see Avoiding pitfalls with closures. for information on the performance impact of extending the scope chain, and for a claim that anonymous functions are “slower” than named functions.

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

Sidebar

Related Questions

I've read in some places that JavaScript strings are UTF-16, and in other places
At some point Find All References feature got broken for a single solution that
I've read that Ruby is great for domain specific languages. In the past few
At some point in the past, in my VB6 days, I remember being able
At some point in our past branches of development in git were merged. However,
At some point of my program I have an atom formed by what previously
At some point I need to display a disabled (greyed out by disabled=disabled attribute)
At some point, I've sent an email and it is processed and received by
At some point I must have moved files from one location to another using
In my application at some point, I have to call the Propel library version

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.