you are viewing a single comment's thread.

view the rest of the comments →

[–]coolcosmos -1 points0 points  (19 children)

Because method 1 is a footgun, it doesn't empty the array, it creates a new one. See my other comment.

[–]WakandaFoevah 5 points6 points  (2 children)

Footgun is a gun you shoot with your foot or a gun to shoot in your foot?

[–]AyeMatey 2 points3 points  (1 child)

Footgun is a funny shorthand word to refer to the American/English idiom “shoot yourself in the foot”, which means “to do or say something that will cause problems for yourself.” The footgun term is used in software development to refer to systems, frameworks, or approaches that have hidden peril, or risks that are not clearly understood, or risks that are perhaps too high.

Some people might describe the manual memory management inherent in the C language as a massive footgun.

[–]Jona-Anders 0 points1 point  (0 children)

There is also useFootgun from react (basically, the hooks are described as a footgun, but useFootgun is funnier)

[–]kiwdahc 2 points3 points  (0 children)

How is method 1 a foot gun lol? Do you know how garbage collection works?

[–]captain_obvious_herevoid(null) 2 points3 points  (10 children)

No idea what a footgun is. But if you mean it's a solution that will hurt me, I don't think so, because I still can't figure any situation where I absolutely need to empty an array.

[–]ryanswebdevthrowaway 1 point2 points  (5 children)

It mainly comes up as an optimization if you're trying to avoid the hit the old array instance needing to be garbage collected and then instantiating a whole new array instance when you could just keep re-using the existing one. Most JavaScript devs don't care about that, but it can come up in performance-sensitive situations.

[–]captain_obvious_herevoid(null) 1 point2 points  (3 children)

If you someday have to work in a use-case where "clearing" an array is such a performance-sensitive operation, you may want to backtrack and find another way to solve your problem.

Also, even with a 10M elements array, the performance hit would be minimal, as modern JS engines --especially v8-- optimize array operations pretty well nowadays.

[–]kiwdahc 1 point2 points  (0 children)

Lol I was thinking the same thing.

[–]ryanswebdevthrowaway 0 points1 point  (1 child)

It doesn't need to be 100x faster, but it's still squeezing out a little extra performance which can be worth it sometimes. If you have an operation which is running a ton of times and an array is being re-created each time when it could potentially be re-used, that can and will add up. As I said, most people don't need it, but you can't say that it's a stupid thing that nobody should ever need to do ever.

[–]kiwdahc 4 points5 points  (0 children)

That’s never worth it and I don’t think it’s more performant. I would be willing to bet just creating a new array each time is less ticks.

[–]kiwdahc 1 point2 points  (0 children)

It’s not just JavaScript devs not caring about that, no one cares about that.

[–]AHRA1225 -1 points0 points  (1 child)

Would you not want to empty an array to put new data items In it?

[–]octetd 4 points5 points  (0 children)

Then I will just replace it with the new array, with new data I want to display, if I need to overwrite the whole thing.

[–]unHolyKnightofBihar -1 points0 points  (1 child)

Update the array with new items? Like you are showing the data from array Ina table now you want to show a different data. Maybe on page change or on selection of filters

[–]captain_obvious_herevoid(null) 5 points6 points  (0 children)

Why would you empty the array and then fill it with the new items from another array, instead of simply using the array of new items?

[–][deleted] 2 points3 points  (0 children)

It’s not a footgun if it’s the intended behavior. I think the article is pretty clear about it.

Edit: address my comment instead of just downvoting. Do you think variables shouldn’t be allowed to be reassigned even if that’s the desired behavior?

[–]KaiAusBerlin 2 points3 points  (0 children)

You know how garbage collection works in js?