Skip to content

TypeSafe Collections #12611

@iRon7

Description

@iRon7

Type of issue

Missing information

Feedback

TypeSafe Collections

PowerShell is a loosely language by nature which often makes life easier but where it concerns (list) collections you might consider to use a TypeSafe collections knowing that they consume less memory and are about 25% faster where it comes to methods as e.g. Add():

$Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$ListInt = [System.Collections.Generic.List[int]]::new()
for ($i = 0; $i -lt 1000000; $i++) {
    $ListInt.Add($i)
}
$Stopwatch.Stop()
Write-Host "Time taken to add 1,000,000 integers to List[int]: $($Stopwatch.Elapsed.TotalMilliseconds) ms"

$Stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$ListObject = [System.Collections.Generic.List[Object]]::new()
for ($i = 0; $i -lt 1000000; $i++) {
    $ListObject.Add($i)
}
$Stopwatch.Stop()
Write-Host "Time taken to add 1,000,000 integers to List[Object]: $($Stopwatch.Elapsed.TotalMilliseconds) ms"

Page URL

https://learn.microsoft.com/en-us/powershell/scripting/dev-cross-plat/performance/script-authoring-considerations?view=powershell-7.5

Content source URL

https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/docs-conceptual/dev-cross-plat/performance/script-authoring-considerations.md

Author

@sdwheeler

Platform Id

319d7280-c701-6893-7caf-ea95ae00e437

Document Id

9818d91b-8c5f-337a-f5e1-7a40abd5fd1a

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions