|
1 |
| -function Foreach-ObjectFast |
| 1 | +function Invoke-PSOneForeach |
2 | 2 | {
|
3 | 3 | <#
|
4 | 4 | .SYNOPSIS
|
5 |
| - Faster Foreach-Object |
| 5 | + Faster ForEach-Object |
6 | 6 |
|
7 | 7 | .DESCRIPTION
|
8 |
| - Foreach-ObjectFast can replace the built-in Foreach-Object and improves pipeline speed considerably. |
9 |
| - Foreach-ObjectFast supports only the most commonly used parameters -Begin, -Process, and -End, so you can replace |
| 8 | + Invoke-PSOneForeach can replace the built-in ForEach-Object and improves pipeline speed considerably. |
| 9 | + Invoke-PSOneForeach supports only the most commonly used parameters -Begin, -Process, and -End, so you can replace |
10 | 10 |
|
11 |
| - 1..100 | Foreach-Object { 'Server{0:d3}' -f $_ } |
| 11 | + 1..100 | ForEach-Object { 'Server{0:d3}' -f $_ } |
12 | 12 |
|
13 | 13 | with
|
14 | 14 |
|
15 |
| - 1..100 | Foreach-ObjectFast { 'Server{0:d3}' -f $_ } |
| 15 | + 1..100 | Invoke-PSOneForeach { 'Server{0:d3}' -f $_ } |
16 | 16 |
|
17 |
| - but you cannot currently replace instances of Foreach-Object that uses the less commonly used parameters, |
| 17 | + but you cannot currently replace instances of ForEach-Object that uses the less commonly used parameters, |
18 | 18 | like -RemainingScripts, -MemberNames, and -ArgumentList
|
19 | 19 |
|
20 |
| - Foreach-ObjectFast has a performance benefit per iteration, so the more objects |
| 20 | + Invoke-PSOneForeach has a performance benefit per iteration, so the more objects |
21 | 21 | you send through the pipeline, the more significant performace benefits you will see.
|
22 | 22 |
|
23 |
| - Foreach-ObjectFast is using a steppable pipeline internally which performs better. |
| 23 | + Invoke-PSOneForeach is using a steppable pipeline internally which performs better. |
24 | 24 | However because of this, the debugging experience will be different, and internal
|
25 | 25 | variables such as $MyInvocation may yield different results. For most every-day tasks,
|
26 | 26 | these changes are not important.
|
27 | 27 |
|
28 |
| - A complete explanation of what Where-ObjectFast does can be found here: |
| 28 | + A complete explanation of what Invoke-PSOneWhere does can be found here: |
29 | 29 | https://powershell.one/tricks/performance/pipeline
|
30 | 30 |
|
31 | 31 | .EXAMPLE
|
32 | 32 | $stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
|
33 | 33 |
|
34 |
| - $result = 1..1000000 | Foreach-ObjectFast -Process { |
| 34 | + $result = 1..1000000 | Invoke-PSOneForeach -Process { |
35 | 35 | "I am at $_"
|
36 | 36 | }
|
37 | 37 |
|
38 | 38 | $report = '{0} elements in {1:n2} seconds'
|
39 | 39 | $report -f $result.Count, $stopwatch.Elapsed.TotalSeconds
|
40 | 40 |
|
41 | 41 | Demos the speed improvements. Run this script to see how well it performs,
|
42 |
| - then replace Foreach-ObjectFast with the default Foreach-Object, and check out |
| 42 | + then replace Invoke-PSOneForeach with the default ForEach-Object, and check out |
43 | 43 | the performace difference. $result is the same in both cases.
|
44 | 44 |
|
45 | 45 | .LINK
|
46 | 46 | https://powershell.one/tricks/performance/pipeline
|
47 |
| - https://github.com/TobiasPSP/Modules.PSOneTools/blob/master/PSOneTools/1.2/Foreach-ObjectFast.ps1 |
| 47 | + https://github.com/TobiasPSP/Modules.PSOneTools/blob/master/PSOneTools/1.2/Invoke-PSOneForeach.ps1 |
48 | 48 | #>
|
49 | 49 |
|
50 | 50 | param
|
|
0 commit comments