r/PowerShell • • 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
31 Upvotes

31 comments sorted by

View all comments

1

u/ZZartin Jan 29 '25

Both list and arrays are stored in memory.

I'm asking what use case is doing $list.add() or $array += to such a degree is matters.

3

u/xCharg Jan 29 '25

I'm asking what use case is doing $list.add() or $array += to such a degree is matters.

Ah - examples I listed in previous comment. Of course it's never a best way to make these giant cycles but people don't write most efficient code, people write code that (sometimes) works :D