0
function findSequence(goal) {
   var find = function (start, history) {
    if (start == goal)
      return history;
    else if (start > goal)
      return null;
    else
      return find(start + 5, "(" + history + " + 5)") ||
             find(start * 3, "(" + history + " * 3)");
  }
  return find(1, "1");
}

var sequence = findSequence(24);

Is sequence a closure function? If yes, is this preferable to use closures in this way? I was taught by web resources to avoid closures.

UPDATE:

I was asked in the comments to show web resources. These are more reliable resources that i have seen in the web.

1.MDN - Closures under "Performance considerations".

2.Addy Osmani's Article under "Garbage Collection - Closures".

3.MSDN - see "Closures" section.

4.Stack Overflow Post - see accepted answer.

5.Stack Overflow Post

6.another intresting article - see last two paragraphs.

Community
  • 1
  • 1
user10
  • 5,186
  • 8
  • 43
  • 64
  • 2
    You were taught to avoid closures? We will teach you otherwise. Actually you cannot really avoid them, they're built into the language. Please share a link to that wrong "web resource" so we know to avoid that resource… – Bergi Jul 12 '13 at 11:04
  • "javascript closure memory leak" in google search. – user10 Jul 12 '13 at 11:06
  • [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Closures?redirectlocale=en-US&redirectslug=JavaScript%2FGuide%2FClosures) under ***performance considersations*** section. – user10 Jul 12 '13 at 11:07
  • 1
    Well, then don't use any technology. It could lead to harm if abused :-) – Bergi Jul 12 '13 at 11:07
  • Your comment might suits, if i am an expert in JavaScript. Just trying to get more depth knowledge. I always shy to post things on SO, to face like these questions. – user10 Jul 12 '13 at 11:16
  • Why would a web resource teach you to avoid closures? – Aadit M Shah Jul 12 '13 at 11:17
  • @AaditMShah should i not rely MDN? – user10 Jul 12 '13 at 11:22
  • Then learn about closures. You need to use closures to do anything decently interesting in JavaScript. Of course you can do interesting things in languages which don't have closures like C as well, but it's nothing as interesting as what you can do using closures. – Aadit M Shah Jul 12 '13 at 11:36
  • do you use closures in your code? – user10 Jul 12 '13 at 11:40
  • Everywhere. They make cool higher-order things possible. Also, most callbacks need to close over their parent scope. You probably have used them yourself already without noticing. – Bergi Jul 12 '13 at 11:45
  • @Bergi if possible, give a read at [this article](http://www.bennadel.com/blog/1482-A-Graphical-Explanation-Of-Javascript-Closures-In-A-jQuery-Context.htm). Particularly last two paragraphs. – user10 Jul 12 '13 at 12:33
  • As he stated, will that be in memory until user closes the app? If yes, what is wrong in my fear about closures? – user10 Jul 12 '13 at 12:44
  • @Bergi another interesting [SO Post](http://stackoverflow.com/questions/7451279/does-creating-functions-consume-more-memory) – user10 Jul 13 '13 at 03:06
  • @user10: Not "*until the user closes the app*", but "until the GC runs". No problems (unless you're doing it wrong). That post you found is good, it can be subsumed to "*modern interpreters are better about that case than they used to be, the performance of closure functions was a known issue a couple years ago. Some say to avoid unnecessary closures for this reason, but closures are one of the **most powerful** and **often used** tools in a JS developer's toolkit*". And notice that the statement is from 2011, one engine generation ago. – Bergi Jul 13 '13 at 19:47

4 Answers4

2

No, sequence is no function at all; so it is no closure.

The find function is a closure. It's recursively called and maintaining a reference to the goal variable from the parent scope. Yet it does not outlive the findSequence call, so we don't utilize this feature. The usage of an extra function for recursion is very fine here.

I was taught to avoid closures.

No need for that. Maybe they need little more memory, but don't care about that. If they come in handy (which happens very often due to their power) and you find them useful, then use them. Don't do premature optimisation. Only if you get actual performance problems, you might look into working around them.

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

Is sequence a closure function?

No. It just does recursion within a scope.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

sequence is a variable which is assigned the returned value from findSequence(24);

Tro
  • 897
  • 9
  • 32
0

No, It is not. You are just recursing in the function and returning the result. Closure is created when you do something like this.

function getFun(param){
    var x = 5;
    return function(){
        return param * x; // Here closure is created, variables param and x are retained.
    }
}

var foo = getFun(5);
alert(foo()); // alerts 25
mohkhan
  • 11,925
  • 2
  • 24
  • 27