Foreach-Object and Where-Object are among the most frequently used pipeline cmdlets, yet they tend to be very slow with a large number of iterations.
A classic foreach loop in comparison to the pipeline often is 100x faster.
Suspected Design Flaw? Reason for Slowness:
That's because both take a scriptblock and invoke it via InvokeReturnAsIs() per each iteration. This prevents code and compiler optimizations.
Technical Improvement - how to make Foreach-Object as fast as foreach loops:
By replacing scriptblock invocation via InvokeReturnAsIs() by a steppable pipeline, Foreach-Object and Where-Object can be just as fast as foreach loops.
Prototypes and Explanation
A detailed explanation, use-cases, and prototypes are available here:
https://powershell.one/tricks/performance/pipeline
Reactions are currently unavailable
Foreach-Object and Where-Object are among the most frequently used pipeline cmdlets, yet they tend to be very slow with a large number of iterations.
A classic foreach loop in comparison to the pipeline often is 100x faster.
Suspected Design Flaw? Reason for Slowness:
That's because both take a scriptblock and invoke it via InvokeReturnAsIs() per each iteration. This prevents code and compiler optimizations.
Technical Improvement - how to make Foreach-Object as fast as foreach loops:
By replacing scriptblock invocation via InvokeReturnAsIs() by a steppable pipeline, Foreach-Object and Where-Object can be just as fast as foreach loops.
Prototypes and Explanation
A detailed explanation, use-cases, and prototypes are available here:
https://powershell.one/tricks/performance/pipeline