Tslint: [Documentation]: prefer-for-of

Created on 21 Nov 2017  Â·  1Comment  Â·  Source: palantir/tslint

_Rationale of the prefer-for-of rule
«A for(… of …) loop is easier to implement and read when the index is not needed.»_

Well the downside of this rationale is that the performance of the for of loop is very slow when compared to the traditional for (;;) loops in most modern browsers so users I think should be cautioned accordingly of its potentially harmful effects.

Documentation Question

Most helpful comment

The performance depends on your compiler settings. If you are targeting ES5 there will be next to no performance loss.
If you target ES6 or higher or use --downlevelIteration you probably do that for a reason. That reason is most likely correctness. That comes with the cost associated with the ES6 iteration protocol.
Also note that recent browser versions heavily optimize such patterns.

So either you should stick with ES5 for now or just don't use ES6 features if you care that much about performance in (older) browsers.
There are other language constructs in ES6 that used to be poorly optimized: let/const, classes, template strings, arrow functions, ... I don't think that justifies adding a warning to a great deal of rule docs.

I agree that you should definitely know about the drawbacks of your target platform regarding certain language features. And of course you should tune your TSLint config accordingly. That involves a lot of benchmarking and is way out of scope of this project. Please note that such microbenchmarks are not a good indicator for real world preformance.

>All comments

The performance depends on your compiler settings. If you are targeting ES5 there will be next to no performance loss.
If you target ES6 or higher or use --downlevelIteration you probably do that for a reason. That reason is most likely correctness. That comes with the cost associated with the ES6 iteration protocol.
Also note that recent browser versions heavily optimize such patterns.

So either you should stick with ES5 for now or just don't use ES6 features if you care that much about performance in (older) browsers.
There are other language constructs in ES6 that used to be poorly optimized: let/const, classes, template strings, arrow functions, ... I don't think that justifies adding a warning to a great deal of rule docs.

I agree that you should definitely know about the drawbacks of your target platform regarding certain language features. And of course you should tune your TSLint config accordingly. That involves a lot of benchmarking and is way out of scope of this project. Please note that such microbenchmarks are not a good indicator for real world preformance.

Was this page helpful?
0 / 5 - 0 ratings