Reading official docs it's obvious that PowerShell -match
operator is more powerful than -like
(due to regular expressions). Secondly, it seems ~10 times faster according to this article https://www.pluralsight.com/blog/software-development/powershell-operators-like-match.
Are there specific cases when I should prefer -like
instead of -match
? If there not, why at all should I use -like
? Does it exist because of historical reasons?
I've never seen -match
test that much faster than -like
, if at all. Normally I see -like
at about the same or better speed.
But I never rely on one test instance, I usually run through about 10K reps of each.
If your're looking for performance, always prefer string methods if they'll meet the requirements:
$string = '123abc'
(measure-command {
for ($i=0;$i -lt 1e5;$i++)
{$string.contains('3ab')}
}).totalmilliseconds
(measure-command {
for ($i=0;$i -lt 1e5;$i++)
{$string -like '*3ab*'}
}).totalmilliseconds
(measure-command {
for ($i=0;$i -lt 1e5;$i++)
{$string -match '3ab'}
}).totalmilliseconds
265.3494
586.424
646.4878
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With