PowerShell Performance-Test: Like and Equals string comparison methods.

Created May 30, 2023

When using PowerShell, many users often rely on the like comparison in a where statement for querying data.

However, there is an alternative method that can be faster, especially when searching for strings that start with specific letters. This method involves using the 'equals' comparison instead.

To begin, let's create some data to query. I have generated a list of '1048576' words, each consisting of 20 characters:

$BunchOfWords = $(
        $(Get-Random -Minimum 65 -Maximum 90 -Count 20).ForEach({
        }) -join ''

Next, we will perform queries on this list to find words that start with the letter 'w', using both the like and equals methods. We can use the first element in the string array, '$PSItem[0]', to achieve this:

#Where method with -eq
$W2 = $BunchOfWords | where-Object{$psitem[0] -eq "W"}
#Where method with -like
$W = $BunchOfWords | Where-Object{$_ -like "W*"}

To query words with multiple specific characters, we can utilize the 'Substring()' method on '$PSItem':

#Where method with -eq (Substring)
$WED2 = $BunchOfWords | where-Object{$psitem.Substring(0,3) -eq "WED"}
#Where method with -like
$WED = $BunchOfWords | Where-Object{$_ -like "WED*"}

Finally, let's examine the results:

Method RuntimeMS
SingeLetter EQ StringArray pos 0 13350
SingeLetter like Query 16739
MultipleLetter EQ Substring 14713
MultipleLetter like Query 17835

As you can observe, the 'equals' comparison method is consistently around 3 seconds faster when dealing with our large list of words. This holds true even when utilizing a substring in the multiple letter test.

That concludes our performance test.

If you have any thoughts or feedback on this topic, feel free to share them with me on Twitter at Christian Ritter.

Best regards,