0

This is relevant, b.c. I want to test looping structures. What I normally do is put in a simple statement like

i++

in the loop. I do this b.c. I wonder if a smart interpreter will not run empty blocks. For example.

for(var i = 0; i < 10; i++) {
}

might not loop at all as there is nothing in the loop.

so I normally do something like:

for(var i = 0; i < 10; i++) {
    i++;
}

But this does test the i++ statement as well as the loop structure, which I don't want.

  • 2
    Smart interpreters do smart things, so the answer is maybe. Today's JS environments are far more that mere interpreters though. –  Jul 10 '13 at 15:13
  • 2
    Skipping loops like that wouldn't be smart at all, I doubt any interpreter would do that. – bfavaretto Jul 10 '13 at 15:17
  • 1
    @bfavaretto: It could feasibly be optimized to just set the `i` to its final value. But I'd wonder what the point of the loop is then, except to make `i` equal to the final value. –  Jul 10 '13 at 15:19
  • 1
    What is dumb about our comments? What is the practical application of this? What compels your alternative? Why would you run a loop just to increment a value, without any other side effect? –  Jul 10 '13 at 15:21
  • 1
    @CrazyTrain In this case yes, it would be just a matter of setting `i` to the final value. But you can do all sorts of things in the head of a `for`, like changing many variables, calling functions, etc. I *think* it's expensive for an optimizer to determine if skipping the loop is worth it or not (probably more expensive than just running most loops). And regarding the OP's comment, I think he was talking about me... :) – bfavaretto Jul 10 '13 at 15:25
  • 1
    @bfavaretto: Yeah, that's the problem. They could optimize this particular case, but why would they? Anyone who would actually write code like this doesn't deserve such an optimization. Ultimately there's no sensible basis for this question as far as I can see... but apparently others are dumb for asking about it. –  Jul 10 '13 at 15:32
  • 1
    Can you provide a concrete example of why it matters that the loop is run when it doesn't do anything useful? – rjmunro Jul 10 '13 at 15:48
  • A good optimizer will replace that whole loop with a single i=10; load register with immediate. I not being used after the loop should make the whole thing disappear as dead code. – old_timer Jul 12 '13 at 17:39

2 Answers2

2

Here look at this. Notice the delay when trying to show the alert: http://jsfiddle.net/xs724/

for(var ii = 0; ii < 1000000000; ii++){}
alert("DONE");

I tested this in chrome. It most likely could vary from browser to browser.

JsPerf Link: http://jsperf.com/js-optimizationlooping

kemiller2002
  • 107,653
  • 27
  • 187
  • 244
  • +1. Put it in a jsperf so we can see if all browsers do not make this optimization. –  Jul 10 '13 at 15:14
  • Where is the delay coming from? Is it from jsFiddle loading the page? How much is from the loop? –  Jul 10 '13 at 15:15
  • 2
    @CrazyTrain The delay scales with the upper bound, so change the number to 10 and see how much more quickly it loads. – Igor Jul 10 '13 at 15:17
  • I commented out the loop to see if the delay was from the page load, and it was almost immediate. – kemiller2002 Jul 10 '13 at 15:18
  • @GrailsGuy: My point is that different loads of the same page can vary in time. It's better to exclude such possibilities by timing the execution. Not that that's absolutely perfect, but it's better. –  Jul 10 '13 at 15:23
  • @pure_code.mom: The `run` button still needs to do some loading. I don't think you have a clue about what you're asking. First you ask specifically if the block is run, but then you seem to be concerned if the head statements are run. I will post on your questions just as much as I'd like. You clearly need all the help you can get. –  Jul 10 '13 at 15:37
  • @pure_code.mom: This coming from the guy who can't write a simple test for himself. This is fun. Keep talking. But yes, if you want an accurate test, you need to remove anything else that could interfere with the result. –  Jul 10 '13 at 15:41
  • @pure_code.mom: I'm not forcing it on anyone. By posting a question in a public place, you're inviting people to reply with information, comments or requests for clarification. Almost no one has a problem with this. –  Jul 10 '13 at 15:50
1

The answer is: You never know. There are lots of optimizations going on in modern JavaScript engines. One example is dead code elimination, which skips code that does not influence the end result.

There was a quite interesting controversy about this feature in IE9: http://digitizor.com/2010/11/17/internet-explorer-9-caught-cheating-in-sunspider-benchmark/

But why would you want to run an empty block over and over anyway?

If you want the JavaScript interpreter to simply wait try this answers:
What is the JavaScript version of sleep()?
Sleep in Javascript - delay between actions

Community
  • 1
  • 1
joe776
  • 1,096
  • 14
  • 22
  • +1 "You never know" is the only real answer, unless one would care to study the source of every update. –  Jul 10 '13 at 15:36