Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

How would I ban native forEach loops? #733

Closed
ChrisMBarr opened this issue Oct 13, 2015 · 7 comments
Closed

How would I ban native forEach loops? #733

ChrisMBarr opened this issue Oct 13, 2015 · 7 comments

Comments

@ChrisMBarr
Copy link
Contributor

I've run some tests and found that native for loops are much faster than using someArray.forEach and 3rd party library forEach methods. I know how to ban the functions from jQuery and Underscore, but how do I ban the native one?

"ban": [
    true,
    [ "_", "forEach" ],
    [ "$", "forEach" ],
    [ "???", "forEach" ]    <-----What_goes_here?
]
@jkillian
Copy link
Contributor

Good question. I currently don't think this is possible, as the ban rule just matches by string. It's a good feature request though.

@adidahiya
Copy link
Contributor

related: #327

@myitcv
Copy link
Contributor

myitcv commented Oct 13, 2015

Need #680 for fully type information of the receiver I suspect.

@glen-84
Copy link
Contributor

glen-84 commented Sep 24, 2016

@ChrisMBarr I haven't tested it, but it doesn't look like #1408 will resolve this issue.

@ChrisMBarr ChrisMBarr reopened this Sep 24, 2016
@CSchulz
Copy link
Contributor

CSchulz commented Feb 22, 2017

Have tested it, issue is not resolved yet.

@ajafff
Copy link
Contributor

ajafff commented Feb 22, 2017

There are several problems:

  • You should prefer optimizing your algorithm before you do premature micro optimizations
  • Silppery slope for the rule's implementation: Banning a method on a specific type by the type's name doesn't guarantee that you acutally got the right type... What if you implement your own class Array (sensibility aside, that's a question that needs to be answered)

One possible (although not very nice) workaround would be to use your own modified version of lib.d.ts. That way you can remove forEach from the Array interface and the compiler report usages as errors.

@ChrisMBarr
Copy link
Contributor Author

This is now fixed by #2547 !

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

7 participants