Psscriptanalyzer: Rule to recommend against Lists when using += with Array objects

Created on 31 Jul 2017  路  2Comments  路  Source: PowerShell/PSScriptAnalyzer

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/

Area - Rules Issue - New Rule in progress

Most helpful comment

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)

All 2 comments

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.

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)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

alatas picture alatas  路  7Comments

chriskuech picture chriskuech  路  6Comments

Jawz84 picture Jawz84  路  4Comments

iainbrighton picture iainbrighton  路  3Comments

ShaydeNofziger picture ShaydeNofziger  路  4Comments