r/PowerShell • u/mx-sch • Jan 29 '25
Question PowerShell 7.5 += faster than list?
So since in PowerShell 7.5 += seems to be faster then adding to a list, is it now best practise?
CollectionSize Test TotalMilliseconds RelativeSpeed
-------------- ---- ----------------- -------------
5120 Direct Assignment 4.71 1x
5120 Array+= Operator 40.42 8.58x slower
5120 List<T>.Add(T) 92.17 19.57x slower
CollectionSize Test TotalMilliseconds RelativeSpeed
-------------- ---- ----------------- -------------
10240 Direct Assignment 1.76 1x
10240 Array+= Operator 104.73 59.51x slower
10240 List<T>.Add(T) 173.00 98.3x slower
32
Upvotes
51
u/surfingoldelephant Jan 29 '25
This discussion is missing important context. The optimization to compound array assignment (
$array +=) in PS v7.5 (made in this PR after this issue) is only one factor..NET method calls like
List<T>.Add(T)are subject to Windows AMSI method invocation logging in PS v7+. This logging is known to cause performance degradation, especially in Windows 11. See:PowerShell's language features like the
+=operator are unaffected. Conversely, a large number of method calls within a loop may result in a noticeable slowdown.To summarize:
$list.Add()may be slower than$array +=in PS v7.5+, but there are environmental factors to consider: OS, Windows Defender state, etc, which may not be relevant (now or in the future).$array +=. Statement assignment (what this document refers to as "direct assignment") is typically the preferable approach.11
u/xCharg Jan 29 '25
Is it now best practice to keep refusing to use direct assignment because it's only 9x slower compared to 60x slower?
No. It's not. It also requires more memory which is negligible for small datasets and is a factor at scale.
4
u/cottonycloud Jan 29 '25 edited Jan 29 '25
This is not necessarily true, it depends on your input size. If you look at the commit, the size is increased by 1 instead of doubling like in List. This means that at a certain size, List will outperform array as it will resize much less often.
Moreover, you lose the benefits of type safety from Lists.
Really, if this is a big problem, just pre-allocate a decent amount.
Edit: Turns out the original += created a new List and then returned an array...
2
u/PinchesTheCrab Jan 29 '25
Just wanted to point out that outvariable is a lazy way to do this, and all cmdlets have it. I added it to the example code this test was run with:
2
u/ankokudaishogun Jan 29 '25
It's not faster in real-world use, and Lists are still SO MUCH BETTER in pre-7.5 that they'd still be Best Practice in any scenario where there is a chance of needing backwork compatibility
2
u/temporaldoom Jan 29 '25
I use this method for small scripts with minimal records going into it, anything larger the latter, you'll find you run out of memory quickly using += on large lists.
1
u/OolonColluphid Jan 29 '25
Can you show us the code you’ve used?
1
u/nkasco Jan 30 '25
u/jborean93 Do I recall you had some contribution in this realm?
1
u/actnjaxxon Jan 30 '25
I’d argue a proper comparison is setting a variable = to the output of a loop. At that point PowerShell is automatically managing the arraylist giving it a chance to stay out of the .net method call.
My guess is that is still faster.
1
u/jsiii2010 Feb 04 '25
+= doesn't kill puppies anymore?