Graph and PowerShell Blog | About | Links
Hash versus Generic List speed test
19-Aug-24

In my PowerShell scripts, I often use arrays to store and sort information. Generally, I use a Generic List with a psCustomObject to cover multiple keys and values, but for some data, I simply need to store a string and match it to a value. Recently I've been cleaning up my scripts and looking for more efficient ways of checking data.

If I take a well-used example, I often get all the users in Active Directory (approx. 2,000 users) and check it against other data. So which is quicker, a generic list of type [string] or a has array?

$allResults = @{}
$userList = [System.Collections.Generic.List[string]]::new()

$adusers = get-aduser -filter *
foreach ($user in $adusers)
{
$count++
$upn = $user.userprincipalname

$allresults.Add($count,$upn)
$userList.add($upn)
}

measure-command -expression { if ($allresults.Values -contains "user1@domain.com") { write-host "found hash" } }
measure-command -expression { if ($userList -contains "user1@domain.com") { write-host "found string" } }

The results show that both are very quick, but the Generic.List is quicker by about 25%:
Hash - TotalMilliseconds : 31.7834
Generic.List - TotalMilliseconds : 24.0008

In subsequent tests the results were a bit closer, but still, the Generic List came out on top.