Created
May 26, 2023 17:42
-
-
Save petervandivier/19a8ff90ed8b3961d072d49e4d6cd705 to your computer and use it in GitHub Desktop.
ValueFromPipeline processing in a function is serial unless you use a steppable pipeline
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -PSEdition Core | |
# HT @santisq in powershell-slack | |
# https://powershell.slack.com/archives/C1RCWRDL4/p1685120926413089 | |
function foo { | |
param( | |
[Parameter(Mandatory,ValueFromPipeline)] | |
[string] | |
$bar | |
) | |
begin { | |
$ScriptBlock = {"input is $_"; Start-Sleep 5} | |
$pipe = { ForEach-Object -Parallel $ScriptBlock }.GetSteppablePipeline() | |
$pipe.Begin($PSCmdlet) | |
} | |
process { | |
$pipe.Process($bar) | |
} | |
end { | |
$pipe.End() | |
} | |
} | |
$foo = @('a','b','c') | |
Measure-Command {$foo | foo} | select TotalSeconds | |
Measure-Command {foo $foo} | select TotalSeconds |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment