The following CoffeeScript code:
for a in arr
do_something_with arr.length
is compiled to:
var a, _i, _len;
for (_i = 0, _len = arr.length; _i < _len; _i++) {
a = arr[_i];
do_something_with(arr.length); // I want to use '_len' here
}
Is that possible to use the cached value of arr.length rather than calculating it in every iteration?
If you have a defined array and not a ranged array, then you could actually use _len in the block:
however, this relies on the CS internals, which could change in a new version.
It also does not work for every array.
but:
I think if you want to used a cached version of the array length, it is better to do that explicitly in your code and not depend on the internal variable names of CS.
rather do: