question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rules proposal: `no-array-shift` and `no-array-unshift`

See original GitHub issue

I’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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
sindresorhuscommented, May 18, 2021

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.

1reaction
fabiospampinatocommented, May 23, 2021

I was not aware of this… So thanks. But isn’t this a V8 bug then? Is there an issue for it there?

I haven’t searched the issue tracker for this, it sure sounds like a bug to me 🤷‍♂️

Read more comments on GitHub >

github_iconTop Results From Across the Web

array_unshift - Manual - PHP
array_unshift () prepends passed elements to the front of the array . Note that the list of elements is prepended as a whole,...
Read more >
array_unshift not working on a multidimensional array, PHP
The error says that the first argument has to be array, not an array element. So it's just: array_unshift($array, 'name'); <---add name to ......
Read more >
Array.prototype.unshift() - JavaScript - MDN Web Docs
The unshift() method adds one or more elements to the beginning of an array and returns the new length of the array.
Read more >
PHP array_unshift() Function - W3Schools
The array_unshift() function inserts new elements to an array. The new array values will be inserted in the beginning of the array.
Read more >
how to shift elements in an array up or down without changing ...
Our project is comparing images, so in order to compare them properly, we covert the images to arrays with the same size and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found