Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to approved verbs & add aliases #10

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@
function Foreach-ObjectFast
function Invoke-PSOneForeach
{
<#
.SYNOPSIS
Faster Foreach-Object
Faster ForEach-Object

.DESCRIPTION
Foreach-ObjectFast can replace the built-in Foreach-Object and improves pipeline speed considerably.
Foreach-ObjectFast supports only the most commonly used parameters -Begin, -Process, and -End, so you can replace
Invoke-PSOneForeach can replace the built-in ForEach-Object and improves pipeline speed considerably.
Invoke-PSOneForeach supports only the most commonly used parameters -Begin, -Process, and -End, so you can replace

1..100 | Foreach-Object { 'Server{0:d3}' -f $_ }
1..100 | ForEach-Object { 'Server{0:d3}' -f $_ }

with

1..100 | Foreach-ObjectFast { 'Server{0:d3}' -f $_ }
1..100 | Invoke-PSOneForeach { 'Server{0:d3}' -f $_ }

but you cannot currently replace instances of Foreach-Object that uses the less commonly used parameters,
but you cannot currently replace instances of ForEach-Object that uses the less commonly used parameters,
like -RemainingScripts, -MemberNames, and -ArgumentList

Foreach-ObjectFast has a performance benefit per iteration, so the more objects
Invoke-PSOneForeach has a performance benefit per iteration, so the more objects
you send through the pipeline, the more significant performace benefits you will see.

Foreach-ObjectFast is using a steppable pipeline internally which performs better.
Invoke-PSOneForeach is using a steppable pipeline internally which performs better.
However because of this, the debugging experience will be different, and internal
variables such as $MyInvocation may yield different results. For most every-day tasks,
these changes are not important.

A complete explanation of what Where-ObjectFast does can be found here:
A complete explanation of what Invoke-PSOneWhere does can be found here:
https://powershell.one/tricks/performance/pipeline

.EXAMPLE
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()

$result = 1..1000000 | Foreach-ObjectFast -Process {
$result = 1..1000000 | Invoke-PSOneForeach -Process {
"I am at $_"
}

$report = '{0} elements in {1:n2} seconds'
$report -f $result.Count, $stopwatch.Elapsed.TotalSeconds

Demos the speed improvements. Run this script to see how well it performs,
then replace Foreach-ObjectFast with the default Foreach-Object, and check out
then replace Invoke-PSOneForeach with the default ForEach-Object, and check out
the performace difference. $result is the same in both cases.

.LINK
https://powershell.one/tricks/performance/pipeline
https://github.com/TobiasPSP/Modules.PSOneTools/blob/master/PSOneTools/1.2/Foreach-ObjectFast.ps1
https://github.com/TobiasPSP/Modules.PSOneTools/blob/master/PSOneTools/1.2/Invoke-PSOneForeach.ps1
#>


# creates command shortcuts for the function
[Alias('ForEach-ObjectFast','ForEachObj')]

param
(
# executes for each pipeline element
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@


function Group-ObjectFast
function Invoke-PSOneGroup
{
# creates command shortcuts for the function
[Alias('Group-ObjectFast','GroupObj')]

[CmdletBinding(DefaultParameterSetName='Analysis')]
param
(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,55 @@
function Where-ObjectFast
function Invoke-PSOneWhere
{
<#
.SYNOPSIS
Faster Where-Object

.DESCRIPTION
Where-ObjectFast can replace the built-in Where-Object and improves pipeline speed considerably.
Where-ObjectFast supports only the scriptblock version of Where-Object, so you can replace
Invoke-PSOneWhere can replace the built-in Where-Object and improves pipeline speed considerably.
Invoke-PSOneWhere supports only the scriptblock version of Where-Object, so you can replace

Get-Service | Where-Object { $_.Status -eq 'Running' }

with

Get-Service | Where-ObjectFast { $_.Status -eq 'Running' }
Get-Service | Invoke-PSOneWhere { $_.Status -eq 'Running' }

but you cannot currently replace the short form of Where-Object:

Get-Service | Where-Object Status -eq Running

Where-ObjectFast has a performance benefit per iteration, so the more objects
Invoke-PSOneWhere has a performance benefit per iteration, so the more objects
you send through the pipeline, the more significant performace benefits you will see.

Where-ObjectFast is using a steppable pipeline internally which performs better.
Invoke-PSOneWhere is using a steppable pipeline internally which performs better.
However because of this, the debugging experience will be different, and internal
variables such as $MyInvocation may yield different results. For most every-day tasks,
these changes are not important.

A complete explanation of what Where-ObjectFast does can be found here:
A complete explanation of what Invoke-PSOneWhere does can be found here:
https://powershell.one/tricks/performance/pipeline

.EXAMPLE
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()

$result = 1..1000000 | Where-ObjectFast -FilterScript {
$result = 1..1000000 | Invoke-PSOneWhere -FilterScript {
$_ % 5
}

$report = '{0} elements in {1:n2} seconds'
$report -f $result.Count, $stopwatch.Elapsed.TotalSeconds

Demos the speed improvements. Run this script to see how well it performs,
then replace Where-ObjectFast with the default Where-Object, and check out
then replace Invoke-PSOneWhere with the default Where-Object, and check out
the performace difference. $result is the same in both cases.

.LINK
https://powershell.one/tricks/performance/pipeline
https://github.com/TobiasPSP/Modules.PSOneTools/blob/master/PSOneTools/1.2/Where-ObjectFast.ps1
https://github.com/TobiasPSP/Modules.PSOneTools/blob/master/PSOneTools/1.2/Invoke-PSOneWhere.ps1
#>

# creates command shortcuts for the function
[Alias('Where-ObjectFast','WhereObj')]

param
(
Expand Down
Binary file modified PSOneTools/2.4/PSOneTools.psd1
Binary file not shown.
6 changes: 3 additions & 3 deletions PSOneTools/2.4/module.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
. $PSScriptRoot\Assert-PSOneFolderExists.ps1
. $PSScriptRoot\Test-PSOnePort.ps1
. $PSScriptRoot\Test-PSOnePing.ps1
. $PSScriptRoot\Foreach-ObjectFast.ps1
. $PSScriptRoot\Where-ObjectFast.ps1
. $PSScriptRoot\Invoke-PSOneForeach.ps1
. $PSScriptRoot\Invoke-PSOneWhere.ps1
. $PSScriptRoot\Test-PSOneScript.ps1
. $PSScriptRoot\Get-PSOneToken.ps1
. $PSScriptRoot\Expand-PSOneToken.ps1
. $PSScriptRoot\Get-PSOneDirectory.ps1
. $PSScriptRoot\Group-ObjectFast.ps1
. $PSScriptRoot\Invoke-PSOneGroup.ps1
. $PSScriptRoot\Find-PSOneDuplicateFile.ps1
. $PSScriptRoot\Show-PSOneApplicationWindow.ps1
. $PSScriptRoot\Get-PSOneClipboardListenerStatus.ps1
Expand Down