0

I've been starting to use IIFEs within for-loops, like so:

var currentItem = 2; // IIFE helps prevent variable collision
for(var i = 0; myArray.length < i; i++){
 (function(){
 var currentItem = myArray[i];
 // do stuff
 })();
}

The main reason I've been doing this is to give some scope to the for-loop, so the variables don't escape this scope. Are there any negative effects/performance hits of doing so? And if so, what is the extent of their harm? Thanks!

asked May 27, 2015 at 21:34
23
  • 4
    Why not just use .forEach()? Commented May 27, 2015 at 21:35
  • 1
    There is a "performance hit": creating and invoking an anonymous function is not free. Btw, what is the real reason to do so? Variables (like currentItem and i) are visible after the loop, so what? Commented May 27, 2015 at 21:36
  • 2
    There's no advantage to doing that because you aren't capturing any variables inside the function. You're reaching to the wider scope that the for loop exists in for i. Commented May 27, 2015 at 21:37
  • 4
    @FillipPeyton: If you don't feel any effects on your program, then it's not enough to care. You can measure performance of your application with both approaches if you're interested. Commented May 27, 2015 at 21:42
  • 1
    @Jordan for primitive numbers - that's what they do. It's tiny, but it's performed. Commented May 27, 2015 at 21:46

1 Answer 1

3

Yes there is a performance cost in wrapping the body of a for statement into an anonymous function. The problem is the cost of creating a new function object for each iteration. That is, for each iteration a new function object is created with its own scope.

Performance comparison named vs anonymous function This is a small comparison between named function and function declaration which may help you to understand the impact over the performance of your code. Note, in your case, the problem is not the anonymous function, but rather the cost of creating the function object.


ES6

I understand, you are trying to scope your variable to the for. I would suggest another solution to achieve this. If you can, try to use let keyword from ES6.

The let keyword has been introduced to overcome all of var's defects, such as this one. let allows you to declares variable that are limited in scope to the for block.

From the documentation:

let allows you to declare variables that are limited in scope to the block, statement, or expression on which it is used. This is unlike the var keyword, which defines a variable globally, or locally to an entire function regardless of block scope.

Barney
2,3714 gold badges23 silver badges38 bronze badges
answered May 27, 2015 at 21:52
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for providing an actual answer instead of a comment. This is actually helpful :)
You don't invoke an anonymous function in the first case, so you compare different things.
let sounds pretty great! Can't wait to see it pick up more traction as far as browser support goes!

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.