Rules proposal: `no-array-shift` and `no-array-unshift`
See original GitHub issueI’d like to have rules that forbid me from using Array.prototype.shift
and Array.prototype.unshift
because they have truly ridiculous performance characteristics in v8 for some reason, and it’s too easy to shoot myself in the foot by using these.
Also I’d imagine a lot of people aren’t even aware of this, having rules for this in eslint-plugin-unicorn
could kind of spread the word on this, which I guess would be a positive thing too.
Fail
const foo = [];
foo.shift (); // Never use shift
foo.unshift (); // Never use unshift
Pass
Everything else basically, it’s difficult to enumerate all the much more performant alternatives one might adopt, but general just iterating over the whole array and letting the GC collect it at the end would be much better. Or even reversing the array and pushing/popping values would be so much faster. Basically as long as the array isn’t resized from its start performance will be reasonable.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:12 (2 by maintainers)
Top GitHub Comments
I disagree that shift/unshift is always bad. In practice, most of my usage of shift/unshift are on small arrays not in a hot-loop, so it’s fine.
If we do decide to add this rule, it would be off by default.
I haven’t searched the issue tracker for this, it sure sounds like a bug to me 🤷♂️