Skip to content

Rule to recommend against Lists when using += with Array objects #806

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
joeyaiello opened this issue Jul 31, 2017 · 2 comments
Open

Rule to recommend against Lists when using += with Array objects #806

joeyaiello opened this issue Jul 31, 2017 · 2 comments

Comments

@joeyaiello
Copy link
Contributor

As some of the below articles note, using += with an Array object can be very slow, as it copies the Array into a new Array in-memory. The recommendation is very often to use List objects instead of Arrays to avoid this problem.

Thoughts?

https://stackoverflow.com/questions/14620290/powershell-array-add-vs
https://powershell.org/2013/09/16/powershell-performance-the-operator-and-when-to-avoid-it/

@nightroman
Copy link

When used in a loop, it's probably a good idea. Otherwise, it may be annoying because there are many reasonable applications of += that may be even faster or just cleaner. New-Object System.Collections.Generic.List[System.Object] is quite an expression to type, parse, evaluate.

@bergmeister
Copy link
Collaborator

Good idea in general but I also agree with @nightroman that one does not care in most situations. I think it would make sense to group PSScriptAnalyzer warnings into something like 3 categories so that one can either configure PSScriptanalyzer to apply only rules of those categories or alternatively filter for them later on:

  • CodeAnalysis (where stuff can go wrong like e.g. PSPossibleIncorrectComparisonWithNull)
  • Best Practices (e.g. PSUseShouldProcessForStateChangingFunctions)
  • PerformanceAnalysis (your array example)

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

Successfully merging a pull request may close this issue.

3 participants