-Name is a convenient switch to only output item names (without -Recurse) or relative paths (with -Recurse).
Given that only string instances rather than System.IO.File/DirectoryInfo instances are output, one would expect -Name to speed things up (or at least not to slow things down); however, with -Recurse present, -Name slows down the command noticeably.
Steps to reproduce (on Windows)
# Warm up the cache
$null = Get-ChildItem -Recurse 'C:\Program Files'
# Without -Name
(Measure-Command { Get-ChildItem -Recurse 'C:\Program Files' }).TotalSeconds
# With -Name
(Measure-Command { Get-ChildItem -Name -Recurse 'C:\Program Files' }).TotalSeconds
Expected behavior
The command with -Name should be faster (or at least not slower).
Actual behavior
Sample timings:
1.544098 # without -Name
4.5759553 # with -Name
That is, adding -Name made the command about 3 times slower.
Environment data
PowerShell Core 6.2.0-preview.4
-Nameis a convenient switch to only output item names (without-Recurse) or relative paths (with-Recurse).Given that only string instances rather than
System.IO.File/DirectoryInfoinstances are output, one would expect-Nameto speed things up (or at least not to slow things down); however, with-Recursepresent,-Nameslows down the command noticeably.Steps to reproduce (on Windows)
Expected behavior
The command with
-Nameshould be faster (or at least not slower).Actual behavior
Sample timings:
That is, adding
-Namemade the command about 3 times slower.Environment data