5358 posts The Dude Abides
  • Located in United States
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
+9 more
CodingJack says

Example #1

var myFunction = (function() {

    var a = {createdJustOnce: "yes"};

    return function() {

        // do something with "a" 

    }

})();

Example #2

function myFunction() {

    var a = {createdJustOnce: "no"};

    // do something with "a"    

}

The way I usually approach memory management is to only store hard references when they’re being used in more than one function. The exceptions I make with JavaScript now are for caching DOM elements and also storing Math equations. But if I need an Object on the fly, let’s say for something like this:

jQuery.css({left: 0, top: 0});

I usually don’t store a hard reference to that Object, even if I’m calling the function multiple times. On a side note, I would do so something like this:

function myFunction() {

    var style = {left: 0, top: 0};

    elA.css(style);
    elB.css(style);

}

As there doesn’t seem much point in creating that Object twice in a row, but I digress.

In Example #2 above, because no hard references are stored, the Objects created are in theory eligible for garbage collection. But in Example #1, the Object created is not.

Keep in mind I’m not talking about loops or functions that fire 100 times a second (for those cases I’d create a hard reference that could be read inside the loop, and then just set the Object to null when I was done with it).

So what’s better? Does it just depend on how large your App is? For example, if you had a huge app and wanted to always use Example #1, you could end up with a huge amount of things stored in memory. And in a case like that, wouldn’t using Example #2 be better as the garbage collector would have more opportunities to release memory and subsequently keeping memory essentially lower?

5358 posts The Dude Abides
  • Located in United States
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
+9 more
CodingJack says

After re-reading what I wrote, probably should have stayed away from the term “hard reference” and instead just said “something that’s eligible for garbage collection vs. something that isn’t. Even though most people will figure out what I was driving at, the two aren’t really the same thing.

320 posts
  • Has been part of the Envato Community for over 5 years
  • Has sold $1,000+ on Envato Market
  • Had an item featured in an Envato Bundle
  • Has collected 1+ items on Envato Market
+2 more
jwmcpeak says

It comes down to how often you’ll call myFunction(). If you call it multiple times throughout your application, then Example 1 is more efficient.

5358 posts The Dude Abides
  • Located in United States
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $125,000+ on Envato Market
  • Has been part of the Envato Community for over 5 years
+9 more
CodingJack says

It comes down to how often you’ll call myFunction(). If you call it multiple times throughout your application, then Example 1 is more efficient.

Good to hear your opinion on this. I’ve always understood the idea of closures for protecting code from other unknown script, but have never used them for the purpose of performance before. I like the idea of using this technique because it’s sort of like creating a “private class” which feels a lot more organized.

2028 posts
  • Made it to the Authors' Hall of Fame
  • Had an item featured on Envato Market
  • Provided great feedback to improve the user experience on Envato Market
  • Interviewed on an Envato blog
+9 more
revaxarts says

Keep in mind that you can use the first version only after it’s call (since it’s a variable) while the the second example is available immediately within its scope

320 posts
  • Has been part of the Envato Community for over 5 years
  • Has sold $1,000+ on Envato Market
  • Had an item featured in an Envato Bundle
  • Has collected 1+ items on Envato Market
+2 more
jwmcpeak says

Good to hear your opinion on this. I’ve always understood the idea of closures for protecting code from other unknown script, but have never used them for the purpose of performance before. I like the idea of using this technique because it’s sort of like creating a “private class” which feels a lot more organized.

Everything is situational in JavaScript; so, be careful. Closures are incredibly powerful, but they can also cause harm if used improperly. With saying that, they’re usually great for singletons that need to maintain state.


Keep in mind that you can use the first version only after it’s call (since it’s a variable) while the the second example is available immediately within its scope

The first version uses an immediately-invoked function (IIF). So myFunction() is ready to go after JavaScript loads and executes the IIF .

by
by
by
by
by
by