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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:31:24+00:00 2026-06-02T13:31:24+00:00

function foo(a) { if (/* Some condition */) { // perform task 1 //

  • 0
function foo(a) {
    if (/* Some condition */) {
        // perform task 1
        // perform task 3
    }
    else {
        // perform task 2
        // perform task 3
    }
}

I have a function whose structure is similar to the above. I want to abstract task 3 into a function, bar(), but I wish to limit the access of this function to only within the scope of foo(a).

To achieve what I want, is it right to change to the following?

function foo(a) {
    function bar() {
        // Perform task 3
    }

    if (/* Some condition */) {
        // Perform task 1
        bar();
    }
    else {
        // Perform task 2
        bar();
    }
}

If the above is correct, does bar() get redefined every time foo(a) gets called? (I am worrying about waste of CPU resource here.)

  • 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-02T13:31:30+00:00Added an answer on June 2, 2026 at 1:31 pm

    Yes, what you have there is right. Some notes:

    • bar is created on every call to the function foo, but:
      • On modern browsers this is a very fast process. (Some engines may well only compile the code for it once, and then reuse that code with a different context each time; Google’s V8 engine [in Chrome and elsewhere] does that in most cases.)
      • And depending on what bar does, some engines may determine that they can “inline” it, eliminating the function call entirely. V8 does this, and I’m sure it’s not the only engine that does. Naturally they can only do this if it doesn’t change the behavior of the code.
    • The performance impact, if any, of having bar created every time will vary widely between JavaScript engines. If bar is trivial, it will vary from undetectable to fairly small. If you’re not calling foo thousands of times in a row (for instance, from a mousemove handler), I wouldn’t worry about it. Even if you are, I’d only worry about it if I saw a problem on slower engines. Here’s a test case involving DOM operations, which suggests that there is an impact, but a trivial one (probably washed out by the DOM stuff). Here’s a test case doing pure computation which shows a much higher impact, but frankly even, we’re talking a difference of microseconds because even a 92% increase on something that takes microseconds to happen is still very, very fast. Until/unless you saw a real-world impact, it’s not something to worry about.
    • bar will only be accessible from within the function, and it has access to all variables and arguments for that call to the function. This makes this a very handy pattern.
    • Note that because you’ve used a function declaration, it doesn’t matter where you put the declaration (top, bottom, or middle — as long as it’s at the top level of the function, not inside a flow control statement, which is a syntax error), it gets defined before the first line of step-wise code is run.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some third-party Javascript that has statements like this: FOO = function() {
Say I have a function foo that I want to call n times. In
class SomeClass { //some members MemberClass one_of_the_mem_; } I have a function foo( SomeClass
My site is going to have some inline code (when using the foo() function...)
function Foo(){ } Foo.prototype={ foo:'some text' ,bar:function(){ console.log('Want to be able to retrieve foo
I have a dojo define function like this one define([ // some param ]),function(foo,
Consider some function foo : def foo(input) input * 2 end How to get
I know that if I am inside some function foo() which is called somewhere
I have a ColdFusion function foo which takes three args, and the second two
Say I have a function foo: (defun foo (x y &rest args) ...) And

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.