Skip to main content
Stack Overflow
  1. About
  2. For Teams

Timeline for Scope within for loop

Current License: CC BY-SA 3.0

29 events
when toggle format what by license comment
May 28, 2015 at 22:54 review Close votes
Jun 1, 2015 at 0:03
May 28, 2015 at 22:33 comment added JLRishe @FillipPeyton You haven't really explained your requirements, so the best we can do is guess, based on your question, that they are artificial or based on wrong assumptions. If you have your reasons, then tell us what they are if you expect a good answer. I presume you're familiar with what an X-Y problem is? As I imagine you know, performance can be very subjective and inconsistent, so asking a question solely about performance without stating up front any of your reasons for wanting to do this as opposed a more orthodox approach makes this a bad question.
May 27, 2015 at 23:56 comment added Fillip Peyton I would say that it isn't a terrible reason. I have my reasons for needing this, so the reasoning is subjective to each developer/situation. And besides all of that, my question still stands the way I asked it. Yet another ailment of SO: I'm tired of getting answers like "why don't you just change your requirements"?
May 27, 2015 at 23:53 comment added Pointy @FillipPeyton IE8 support is a pretty terrible reason; there are widely available, perfectly serviceable polyfills for .forEach().
May 27, 2015 at 21:52 answer added Giuseppe Pes timeline score: 3
May 27, 2015 at 21:49 comment added Oriol Consider moving the loop inside the IIFE. This way there will only be one function creation and one call, instead of one at each iteration.
May 27, 2015 at 21:48 comment added Felix Kling How about for (...) doStuff(myArray[i]); where function doStuff(currentItem) { // do stuff }. Overall, the answer is: it depends (on the engine).
May 27, 2015 at 21:47 comment added Jordan Running @zerkms Fair enough. ¯\_(ツ)_/¯
May 27, 2015 at 21:46 history edited Fillip Peyton CC BY-SA 3.0
added 26 characters in body
May 27, 2015 at 21:46 comment added zerkms @Jordan for primitive numbers - that's what they do. It's tiny, but it's performed.
May 27, 2015 at 21:46 history edited Fillip Peyton CC BY-SA 3.0
added 26 characters in body
May 27, 2015 at 21:45 comment added Jordan Running Okay, I'll give you that, @zerkms, but do you really think any browser in common use is dumb enough to needlessly copy that value?
May 27, 2015 at 21:45 comment added zerkms @FillipPeyton please demonstrate the exact "collision" (I cannot think of how one could have one honestly)
May 27, 2015 at 21:44 comment added Fillip Peyton @zerkms - First thing that comes to mind is variable collision. I'm sure I can think of other issues that this would help with too.
May 27, 2015 at 21:44 comment added zerkms @Jordan "nor costs memory" --- that's not true. The standard does not require implementations to optimise assigning a scalar values (which might be a 100Mb long string).
May 27, 2015 at 21:43 history edited Fillip Peyton CC BY-SA 3.0
added 53 characters in body
May 27, 2015 at 21:42 comment added Jordan Running The value of currentItem already exists in the outer scope because it exists in myArray. Assigning it to a new variable neither gains nor costs memory (except a negligible amount in a lookup table somewhere)—the value exists only once in memory.
May 27, 2015 at 21:42 comment added zerkms @FillipPeyton it should be the opposite - what is the real problem you're solving with this approach? "I don't like variables having wider scope than they might" is not a problem.
May 27, 2015 at 21:42 comment added Fillip Peyton Good idea. I was just more curious if this was a serious concern or not. I can create some tests.
May 27, 2015 at 21:42 comment added Bergi @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.
May 27, 2015 at 21:40 comment added Bergi Depends on what the // do stuff is whether the IIFE is necessary or just superflouous (and harmful to performance).
May 27, 2015 at 21:40 comment added Fillip Peyton But currentItem isn't reaching the outer scope, and that is my goal. Also, I understand that functions require memory and processing. I guess my follow up questions are: How large of an effect does this have on a program? And is it enough of an effect to rationalize not using the IIFE?
May 27, 2015 at 21:38 comment added Quentin @FillipPeyton — Functions require memory and processing.
May 27, 2015 at 21:37 comment added Quentin 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.
May 27, 2015 at 21:37 comment added Fillip Peyton Care to explain exactly what kind of hit you're speaking of?
May 27, 2015 at 21:36 comment added zerkms 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?
May 27, 2015 at 21:35 comment added Fillip Peyton @Pointy - IE8 support and curiosity.
May 27, 2015 at 21:35 comment added Pointy Why not just use .forEach()?
May 27, 2015 at 21:34 history asked Fillip Peyton CC BY-SA 3.0
toggle format

AltStyle によって変換されたページ (->オリジナル) /