0

This is a general question about Javascript and Node. Suppose I have a function that goes like:

function outer(a,b){
    function inner(){
        console.log(a[b]);
    }
    inner();
}

The outer function will be used many times and it has the arguments like that because I don't want to use global variables. Will the inner function be declared every time I call the outer function? If it is declared everytime, is there a way to make the code more performant without using global variables?

  • You aren't returning the inner function are you? Why not call `console.log` directly? – MinusFour Sep 07 '15 at 18:52
  • Are you looking to just execute the inner function, or do you care for returning it / saving it for an asynchronous call? – Amit Sep 07 '15 at 18:53
  • I want to return the value from the inner function.'a' and 'b' were originally global variables, but now I realize that there values need to change sometimes for 'inner' to work properly. - don't want the 'a' and 'b' to change globally, so I wrapped it in an outer function. Now I am worried about performance. – user3806501 Sep 07 '15 at 19:25

3 Answers3

0

Function inner wil be declared each time you call outer I think you can just write

function outer(a,b){
    console.log(a[b]); 
}

And there is no global variables.

MysterX
  • 2,318
  • 1
  • 12
  • 11
0

Without using static (in JS case global) variables, the function will get declared each time. The approach here is to make an object which encapsulates the functionality:

var AnObject = function(a,b) {
    this.a = a;
    this.b = b;
    this.foo = function() {
       return this.a[this.b];
    };
};

function outer(a,b) {
    var inner = new AnObject(a,b);
    console.log(inner.foo());
}

I hope I understood your question, the code is too trivial to assume more of what you want.

Alex
  • 810
  • 9
  • 16
  • Thanks! This what I was looking for. – user3806501 Sep 07 '15 at 19:31
  • Hell no?! Now you're not only creating a single function, but an object *and* a function every time `outer` is called. – Bergi Sep 07 '15 at 20:33
  • I've just did a memory inspect with chromium engine, calling the outer 10000 times. When the inner function is defined inside the outer function, it assumes you want an inline function (inline like in C++). If you're DEFINING the object outside the call, you're working with references and the compiler effort is smaller compared to the code in question. – Alex Sep 07 '15 at 20:41
  • I was looking for a good way to encapsulate the code so that it could be performant as well as functional. Objects were the way to go. :D – user3806501 Sep 23 '15 at 05:48
0

Will the inner function be declared every time I call the outer function?

Yes. However, this isn't really that inefficient, and is well optimised by engines.

If it is declared everytime, is there a way to make the code more performant without using global variables?

Don't make your code more complicated than it needs to be. If it works and is maintainable as currently written, leave it as it is. outer may be called often, many times, but unless it's a really hot path and you did identify this as a performance bottleneck via benchmarks, there's no need to take action. Premature optimisation is the root of all evil.

But back to your question: Yes, there is a way to avoid the recreation of the function without using global variables. Just pass the values that inner needs as arguments to every call:

function inner(a,b){
    console.log(a[b]);
}
function outer(a,b){
    inner(a,b);
}

Assuming that the inner function doesn't assign to the closure variables this works well.

If you want to avoid putting inner in the global scope, just use the module pattern to create outer.

Community
  • 1
  • 1
Bergi
  • 630,263
  • 148
  • 957
  • 1,375