Skip to content

Instantly share code, notes, and snippets.

@santisq
Created April 27, 2024 21:09
Show Gist options
  • Save santisq/93776c1ddd0bdd02d91bb8f50ae4f87e to your computer and use it in GitHub Desktop.
Save santisq/93776c1ddd0bdd02d91bb8f50ae4f87e to your computer and use it in GitHub Desktop.
$func = Invoke-RestMethod https://gist.githubusercontent.com/santisq/bd3d1d47c89f030be1b4e57b92baaddd/raw/73952eceb82cd31b8cb9f2fd04e2a3ef3c428110/Measure-Expression.ps1
. ([scriptblock]::Create($func))
time -TestCount 5 -OutputAllTests @{
'TestUpdateTypeData' = {
class TestUpdateTypeData {
hidden [int] $_x
hidden [int] $_y
TestUpdateTypeData([int] $x, [int] $y) {
$this._x, $this._y = $x, $y
}
static TestUpdateTypeData() {
$updateTypeDataSplat = @{
MemberName = 'X'
MemberType = 'CodeProperty'
Value = [TestUpdateTypeData].GetMethod('get_X')
SecondValue = [TestUpdateTypeData].GetMethod('set_X')
TypeName = 'TestUpdateTypeData'
}
Update-TypeData @updateTypeDataSplat
$updateTypeDataSplat['MemberName'] = 'Y'
$updateTypeDataSplat['Value'] = [TestUpdateTypeData].GetMethod('get_Y')
$updateTypeDataSplat.Remove('SecondValue')
Update-TypeData @updateTypeDataSplat
}
hidden static [int] get_X([psobject] $instance) {
return $instance._x
}
hidden static [int] get_Y([psobject] $instance) {
return $instance._y
}
hidden static [void] set_X([psobject] $instance, [int] $value) {
$instance._x = $value
}
}
$i = 10000
while ($i--) {
$instance = [TestUpdateTypeData]::new($i, $i)
$instance.X; $instance.Y
}
}
'TestPSCodePropertyAttach' = {
class TestPSCodePropertyAttach {
hidden [int] $_x
hidden [int] $_y
TestPSCodePropertyAttach([int] $x, [int] $y) {
$this._x, $this._y = $x, $y
[TestPSCodePropertyAttach]::InitializeAccessors($this)
}
hidden static [int] get_X([psobject] $instance) {
return $instance._x
}
hidden static [int] get_Y([psobject] $instance) {
return $instance._y
}
hidden static [void] set_X([psobject] $instance, [int] $value) {
$instance._x = $value
}
hidden static [void] InitializeAccessors([psobject] $instance) {
# get; set; for X
$instance.PSObject.Properties.Add(
[System.Management.Automation.PSCodeProperty]::new(
'X',
[TestPSCodePropertyAttach].GetMethod('get_X'),
[TestPSCodePropertyAttach].GetMethod('set_X')))
# get; for Y
$instance.PSObject.Properties.Add(
[System.Management.Automation.PSCodeProperty]::new(
'Y',
[TestPSCodePropertyAttach].GetMethod('get_Y')))
}
}
$i = 10000
while ($i--) {
$instance = [TestPSCodePropertyAttach]::new($i, $i)
$instance.X; $instance.Y
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment