sindresorhus / eslint-plugin-unicorn

More than 100 powerful ESLint rules
MIT License
4.24k stars 367 forks source link

`explicit-length-check`: Possible deprecation or new rule with reversed behaviour #2455

Closed ferferga closed 1 month ago

ferferga commented 1 month ago

Description

I did some benchmarks in different algorithms for checking if a collection has contents: https://jsperf.app/kopiro

Although we're probably taking the side of microoptimization here, as you can see, not doing the comparison with 0 gives +1M (Chromium) or +500K ops (Firefox), so I think it makes sense to at least provide the reversed rule for people desiring to enforce that (and shouldn't require much more logic?).

Fail

if (short.length > 0) {

}

Pass

if (short.length) {

}

Additional Info

No response

sindresorhus commented 1 month ago

This is just a micro optimization and could easily change in a new JS engine version.

https://sindresorhus.com/blog/micro-benchmark-fallacy

sindresorhus commented 1 month ago

I'm going to pass on this.