Skip to content

Speed up Foreach-Object and Where-Object by using Steppable Pipeline #10982

@TobiasPSP

Description

@TobiasPSP

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Discussionthe issue may not have a clear classification yet. The issue may generate an RFC or may be reclassifIssue-Enhancementthe issue is more of a feature request than a bugResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Corecmdlets in the Microsoft.PowerShell.Core moduleWG-Engine-Performancecore PowerShell engine, interpreter, and runtime performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions