最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is the condition in the for loop evaluated in every loop? - Stack Overflow

programmeradmin4浏览0评论

I have a situation like:

for(var i = 0; i < a + b; ++i)
  // code that doesn't affect a and b

Should I worry about the addition been performed in every iteration? Or JavaScript (its parser?) is smart enough to understand that a + b is constant?

In other words, should I do that like this:

var end = a + b;
for(var i = 0; i < end; ++i)
  // code

or will this waste a line of code?


Well, actually what I worry about is not that one line of code, BUT the fact that I am thinking about it every time I face a situation like this in JavaScript! Also, today it's an addition, tomorrow it may be something else, like the square root of it, so I think it's important!

I have a situation like:

for(var i = 0; i < a + b; ++i)
  // code that doesn't affect a and b

Should I worry about the addition been performed in every iteration? Or JavaScript (its parser?) is smart enough to understand that a + b is constant?

In other words, should I do that like this:

var end = a + b;
for(var i = 0; i < end; ++i)
  // code

or will this waste a line of code?


Well, actually what I worry about is not that one line of code, BUT the fact that I am thinking about it every time I face a situation like this in JavaScript! Also, today it's an addition, tomorrow it may be something else, like the square root of it, so I think it's important!

Share Improve this question edited Sep 28, 2015 at 14:20 Andrea Casaccia 4,9714 gold badges33 silver badges55 bronze badges asked Sep 28, 2015 at 13:23 gsamarasgsamaras 73.4k48 gold badges207 silver badges326 bronze badges 12
  • i pretty sure that this evaluate on every iteration – Grundy Commented Sep 28, 2015 at 13:25
  • 2 I would imagine it's evaluated on every line, but it would be down to the optimiser in whatever javascript implementation is running the code. If it's smart enough, it may optimise it out. Once you've profiled the code (properly) and you've found that performing that check yourself is slowing things down, that's the time to worry about optimising it manually. – James Thorpe Commented Sep 28, 2015 at 13:28
  • 2 I would add the line for readability, regardless of the gain in performance, which, btw, would be minor for most current engines. – Jay Commented Sep 28, 2015 at 13:29
  • 3 @illiptic: I would avoid the extra line for the same reason (unless there's a much more descriptive name than end for the variable) :-) Agree that the performance is negligible. – Bergi Commented Sep 28, 2015 at 13:34
  • 2 "I worry about the fact that I am thinking about it every time I face a situation like this": Stop worrying! – Bergi Commented Sep 28, 2015 at 13:36
 |  Show 7 more ments

3 Answers 3

Reset to default 8

The condition is going to be evaluated each time.

From: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Statements/for

for ([initialization]; [condition]; [final-expression])
   statement

condition: An expression to be evaluated before each loop iteration. If this expression evaluates to true, statement is executed. This conditional test is optional. If omitted, the condition always evaluates to true. If the expression evaluates to false, execution skips to the first expression following the for construct.

(Italic is mine)

There could or could not be an optimization on a sub-expression of the condition, depending on the engine: when you have a similar doubt the fastest thing you can do is to setup a test and measure performances.

According to this test for example the two versions take the same time on the latest Chrome and Firefox.

However, the rule of the thumb about this kind of optimizations is: do not prematurely optimize. From Program optimization on Wikipedia:

"We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%"

Knuth, Donald (December 1974). "Structured Programming with go to Statements". ACM Journal Computing Surveys 6 (4): 268. CiteSeerX: 10.1.1.103.6084.

Write functionally correct code, without worrying about such small or doubt performance issues. When executing, if you find you have a performances problem, dig in and optimize.

TL; DR

You shouldn't be worried about this, the performance impact is likely to be minimal, and modern javascript engines seem to optimize this anyway.

But if you are still worried, writing a line of code doesn't seem to me a big waste. So why don't just you do that, and stop thinking about it?

It's better to define the constant like this :

for(var i = 0, end = a + b; i < end; ++i)

Optimization way

You can write your loop like this :

for(var i = a + b; i--;)

This is for me the more optimized but i is descending and not ascending


More example

A simple example to understand. If you create your loop like this :

for(var i = 0; i < array.length; ++i) { 
   array.push('value'); // infinite loop
}

array.length is evaluated on every iteration and you can create an infinite loop with array.push().

It will be evaluated for each loop but a simple addition operation such as the a + b that you used in the example is generally such an easy operation that you won't see a noticeable difference one way or the other. That being said, it is a better idea to just add a and b together before the loop as in your second example.

发布评论

评论列表(0)

  1. 暂无评论