PowerShell array performances

Hello,

I think most of you ever used a PowerShell list like this one :

$ClassicList=@()

Let’s have a look at the performances in heavy load :

1..50000 | % {$ClassicList+=$_}

You’ll notice that it take quite a long time to proceed.

Now, let’s try with an other type of list:

[System.Collections.ArrayList]$ArrayList=@()

Try the same operation with it :

1..50000 | % {$ArrayList.Add($_) | Out-Null}

It’s much faster, let’s have a look at the numbers :

ArrayListPerf

The “ArrayList” is blazing fast, indeed, when you use the “+=” operator, PowerShell, under the hood, create a new object, and put the old and the new one in it.

If you pipe those objects in “Get-Member” :

ArrayListClassicList

 

 

Leave a Reply