jashkenas/coffee-script — Issue #1875: Conditional guards in for loops
Author: rjack
Created: 2011-11-18 11:46:54+00:00
State: open

Hello,

I noticed that this code

for i in [from..to] by step
i

compiles in a sub-optimal `for` loop because it accounts for the general case:

for (i = from; from <= to ? i <= to : i >= to; i += step) {
i;
}

I'd like to tell CS that for some loops `from < to` is always true.

Something simlar to:

for i in [from..to] +by step
i
for (i = from; i <= to; i += step) {
i;
}

and

for i in [from..to] -by step
i
for (i = from; i >= to; i += step) {
i;
}

Maybe it's already possible and I missed it.

Il would be a nice optimization to have when loop execution must be as fast as possible (I discovered this manipulating `canvas` pixels, for example).

JSPerf test case: http://jsperf.com/conditional-guard-in-foor-loop

Cheers,
Giacomo