Skip to content

Instantly share code, notes, and snippets.

@zett42
Last active December 22, 2022 13:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zett42/a63c329bb7be71cded331436dc483edd to your computer and use it in GitHub Desktop.
Save zett42/a63c329bb7be71cded331436dc483edd to your computer and use it in GitHub Desktop.
Measure performance of various ways to flatten (unroll) nested PowerShell objects
<# Copyright 2022 zett42
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files
(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #>
$ErrorActionPreference = 'Stop'
Set-StrictMode -Version 3.0
#-------------------------------------------------------------------------------------------------------------------------------
Function Expand-PropertiesByQueue {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)] [PSObject] $InputObject,
[Parameter()] [string] $Separator = '/'
)
process {
$queue = [Collections.Queue]::new()
$queue.Enqueue( [PSCustomObject]@{ Child = $InputObject; Path = $null } )
while( $queue.Count ) {
$item = $queue.Dequeue()
foreach( $property in $item.Child.PSObject.Properties ) {
$propertyPath = if( $item.Path ) { $item.Path + $Separator + $property.Name } else { $property.Name }
if( $property.Value -is [PSObject] ) {
$queue.Enqueue( [PSCustomObject]@{ Child = $property.Value; Path = $propertyPath } )
continue
}
# Output property
[PSCustomObject]@{ Path = $propertyPath; Value = $property.Value }
}
}
}
}
#-------------------------------------------------------------------------------------------------------------------------------
Function Expand-PropertiesByQueueGeneric {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)] [PSObject] $InputObject,
[Parameter()] [string] $Separator = '/'
)
process {
$queue = [Collections.Generic.Queue[Collections.Generic.KeyValuePair[string, PSObject]]]::new()
$queue.Enqueue( [Collections.Generic.KeyValuePair[string, PSObject]]::new( $null, $InputObject ) )
while( $queue.Count ) {
$item = $queue.Dequeue()
foreach( $property in $item.Value.PSObject.Properties ) {
$propertyPath = if( $item.Key ) { $item.Key + $Separator + $property.Name } else { $property.Name }
if( $property.Value -is [PSObject] ) {
$queue.Enqueue( [Collections.Generic.KeyValuePair[string, PSObject]]::new( $propertyPath, $property.Value ) )
continue
}
# Output property
[PSCustomObject]@{ Path = $propertyPath; Value = $property.Value }
}
}
}
}
#-------------------------------------------------------------------------------------------------------------------------------
Function Expand-PropertiesByStack {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)] [PSObject] $InputObject,
[Parameter()] [string] $Separator = '/'
)
process {
$stack = [Collections.Stack]::new()
$stack.Push( [PSCustomObject]@{
Path = $null
Enumerator = $InputObject.PSObject.Properties.GetEnumerator()
})
:stackLoop while( $stack.Count ) {
$item = $stack.Peek()
while( $item.Enumerator.MoveNext() ) {
$property = $item.Enumerator.Current
$propertyPath = if( $item.Path ) { $item.Path + $Separator + $property.Name } else { $property.Name }
if( $property.Value -is [PSObject] ) {
# Pseudo-recurse into child objects
$stack.Push( [PSCustomObject]@{
Path = $propertyPath
Enumerator = $property.Value.PSObject.Properties.GetEnumerator()
})
continue stackLoop
}
# Output property
[PSCustomObject]@{ Path = $propertyPath; Value = $property.Value }
}
$null = $stack.Pop()
}
}
}
#-------------------------------------------------------------------------------------------------------------------------------
Function Expand-PropertiesByStackGeneric {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)] [PSObject] $InputObject,
[Parameter()] [string] $Separator = '/'
)
process {
$stack = [Collections.Generic.Stack[Collections.Generic.KeyValuePair[string, Collections.IEnumerator]]]::new()
$stack.Push( [Collections.Generic.KeyValuePair[string, Collections.IEnumerator]]::new(
$null,
$InputObject.PSObject.Properties.GetEnumerator()
))
:stackLoop while( $stack.Count ) {
$item = $stack.Peek()
while( $item.Value.MoveNext() ) {
$property = $item.Value.Current
$propertyPath = if( $item.Key ) { $item.Key + $Separator + $property.Name } else { $property.Name }
if( $property.Value -is [PSObject] ) {
# Pseudo-recurse into child objects
$stack.Push( [Collections.Generic.KeyValuePair[string, Collections.IEnumerator]]::new(
$propertyPath,
$property.Value.PSObject.Properties.GetEnumerator()
))
continue stackLoop
}
# Output property
[PSCustomObject]@{ Path = $propertyPath; Value = $property.Value }
}
$null = $stack.Pop()
}
}
}
#-------------------------------------------------------------------------------------------------------------------------------
Function Expand-PropertiesRecursive {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)] [PSObject] $InputObject,
[Parameter()] [string] $Path,
[Parameter()] [string] $Separator = '/'
)
process {
foreach( $property in $InputObject.PSObject.Properties ){
$propertyPath = if( $Path ) { $Path + $Separator + $property.Name } else { $property.Name }
if( $property.Value -is [PSObject] ) {
Expand-PropertiesRecursive $property.Value $propertyPath
continue
}
[PSCustomObject]@{ Path = $propertyPath; Value = $property.Value }
}
}
}
#-------------------------------------------------------------------------------------------------------------------------------
class RecursivePropertyExpander {
static [Object] Expand( [PSObject] $InputObject, [string] $Separator, [string] $Path ) {
$result = foreach( $property in $InputObject.PSObject.Properties ) {
$propertyPath = if( $Path ) { $Path + $Separator + $property.Name } else { $property.Name }
if( $property.Value -is [PSObject] ) {
[RecursivePropertyExpander]::Expand( $Property.Value, $Separator, $propertyPath )
continue
}
# Output property
[PSCustomObject]@{ Path = $propertyPath; Value = $property.Value }
}
return $result
}
}
Function Expand-PropertiesRecursiveClassMethod {
[CmdletBinding()]
param (
[Parameter(Mandatory, ValueFromPipeline)] [PSObject] $InputObject,
[Parameter()] [string] $Separator = '/'
)
process {
[RecursivePropertyExpander]::Expand( $InputObject, $Separator, $null )
}
}
#-------------------------------------------------------------------------------------------------------------------------------
# Sample data
$json = ConvertFrom-Json @'
{
"New": {
"App1": {
"reply_urls": [
"https://testapp1url1"
],
"App1b": {
"reply_urls": [
"https://testapp1url2"
],
"App1c": {
"reply_urls": [
"https://testapp1url3"
]
}
}
},
"App2": {
"reply_urls": [
"https://testapp2url1",
"https://testapp2url2"
],
"App2b": {
"reply_urls": [
"https://testapp2url3"
],
"App2c": {
"reply_urls": [
"https://testapp2url4"
]
}
}
},
"App3": {
"reply_urls": [
"https://testapp3url1",
"https://testapp3url2",
"https://testapp3url3"
],
"App3b": {
"reply_urls": [
"https://testapp3url4"
],
"App3c": {
"reply_urls": [
"https://testapp3url5"
]
}
}
}
},
"Remove": {
"object_id": [
"foobar"
]
}
}
'@
#-------------------------------------------------------------------------------------------------------------------------------
# Test output
Function Write-HeadLine( $s ) { "{0}`n{1}`n{0}" -f ('-' * 80), $s }
Write-HeadLine 'Expand-PropertiesByQueue'
$json | Expand-PropertiesByQueue | Out-Default
Write-HeadLine 'Expand-PropertiesByQueueGeneric'
$json | Expand-PropertiesByQueueGeneric | Out-Default
Write-HeadLine 'Expand-PropertiesByStack'
$json | Expand-PropertiesByStack | Out-Default
Write-HeadLine 'Expand-PropertiesByStackGeneric'
$json | Expand-PropertiesByStackGeneric | Out-Default
Write-HeadLine 'Expand-PropertiesRecursive'
$json | Expand-PropertiesRecursive | Out-Default
Write-HeadLine 'Expand-PropertiesRecursiveClassMethod'
$json | Expand-PropertiesRecursiveClassMethod | Out-Default
#-------------------------------------------------------------------------------------------------------------------------------
# Benchmark
'Running benchmark...'
# Time-Command.ps1 from https://gist.github.com/mklement0/9e1f13978620b09ab2d15da5535d1b27
. $PSScriptRoot\Time-Command.ps1
Time-Command -Count 50000 -ScriptBlock @(
{ $null = $json | Expand-PropertiesByQueue }
{ $null = $json | Expand-PropertiesByQueueGeneric }
{ $null = $json | Expand-PropertiesByStack }
{ $null = $json | Expand-PropertiesByStackGeneric }
{ $null = $json | Expand-PropertiesRecursive }
{ $null = $json | Expand-PropertiesRecursiveClassMethod }
)
Factor Secs (50000-run avg.) Command TimeSpan
------ --------------------- ------- --------
1,00 0,000 $null = $json | Expand-PropertiesByQueueGeneric 00:00:00.0002829
1,12 0,000 $null = $json | Expand-PropertiesByQueue 00:00:00.0003177
1,28 0,000 $null = $json | Expand-PropertiesByStackGeneric 00:00:00.0003611
1,37 0,000 $null = $json | Expand-PropertiesRecursiveClassMethod 00:00:00.0003883
1,41 0,000 $null = $json | Expand-PropertiesByStack 00:00:00.0003979
3,61 0,001 $null = $json | Expand-PropertiesRecursive 00:00:00.0010212
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment