Skip to content

Performance: -join slower than [string]::Join() #10377

@HumanEquivalentUnit

Description

@HumanEquivalentUnit

Steps to reproduce

Joining 100kb random numbers, the operator is much slower, ~13x here:

[string[]]$nums = Get-Random -Minimum 1 -Maximum 100 -Count 100kb

measure-command { $nums -join ','            } |% TotalMilliseconds
measure-command { [string]::join(',', $nums) } |% TotalMilliseconds

234.82690000000002
14.497100000000001

Stranger, with a big wordlist text file, loading it two different ways seems to make a difference:

$w1 = Get-Content d:\test\wordlist.txt;   $w1.Count
$w2 = ${D:\test\wordlist.txt};            $w2.Count

measure-command { $w1 -join "`r`n" } |% TotalMilliseconds
measure-command { $w2 -join "`r`n" } |% TotalMilliseconds

measure-command { [string]::join("`r`n", $w1) } |% TotalMilliseconds
measure-command { [string]::join("`r`n", $w2) } |% TotalMilliseconds

172823
172823        # same word count

254.6534
423.63100000000003    # <-- slower?

183.01510000000002
16.148400000000002    # <-- how come this one is SO fast?

Environment data

PS D:\> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.0.0-preview.1
PSEdition                      Core
GitCommitId                    7.0.0-preview.1
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-By DesignThe reported behavior is by design.WG-Engine-Performancecore PowerShell engine, interpreter, and runtime performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions