PowerShell 7.5 changes
Microsoft's edit ·
Microsoft's commit message in MicrosoftDocs/PowerShell-Docs · commit 4b8a740 · +97 −1 lines · also on GitHub
11
---
22
title: What's New in PowerShell 7.5
33
description: New features and changes released in PowerShell 7.5
4
−ms.date: 11/18/2024
4
+ms.date: 12/05/2024
55
---
66
77
# What's New in PowerShell 7.5
⋯ 142 unchanged lines
150150
([#20402][20402]) (Thanks @domsleee!)
151151
- [PSSerializeJSONLongEnumAsNumber][06] - `ConvertTo-Json` now treats large enums as numbers
152152
([#20999][20999]) (Thanks @jborean93!)
153
+
154
+## Performance improvements
155
+
156
+PowerShell 7.5-rc.1 included [PR#23901][23901] from @jborean93 that improves the performance of the
157
+`+=` operation for an array of objects.
158
+
159
+The following example measures the performance for different methods of adding elements to an array.
160
+
161
+```powershell
162
+$tests = @{
163
+ 'Direct Assignment' = {
164
+ param($count)
165
+
166
+ $result = foreach($i in 1..$count) {
167
+ $i
168
+ }
169
+ }
170
+ 'List<T>.Add(T)' = {
171
+ param($count)
172
+
173
+ $result = [Collections.Generic.List[int]]::new()
174
+ foreach($i in 1..$count) {
175
+ $result.Add($i)
176
+ }
177
+ }
178
+ 'Array+= Operator' = {
179
+ param($count)
180
+
181
+ $result = @()
182
+ foreach($i in 1..$count) {
183
+ $result += $i
184
+ }
185
+ }
186
+}
187
+
188
+5kb, 10kb | ForEach-Object {
189
+ $groupResult = foreach($test in $tests.GetEnumerator()) {
190
+ $ms = (Measure-Command { & $test.Value -Count $_ }).TotalMilliseconds
191
+
192
+[pscustomobject]@{
193
+ CollectionSize = $_
194
+ Test = $test.Key
195
+ TotalMilliseconds = [math]::Round($ms, 2)
196
+ }
197
+
198
+[GC]::Collect()
199
+ [GC]::WaitForPendingFinalizers()
200
+ }
201
+
202
+$groupResult = $groupResult | Sort-Object TotalMilliseconds
203
+ $groupResult | Select-Object *, @{
204
+ Name = 'RelativeSpeed'
205
+ Expression = {
206
+ $relativeSpeed = $_.TotalMilliseconds / $groupResult[0].TotalMilliseconds
207
+ $speed = [math]::Round($relativeSpeed, 2).ToString() + 'x'
208
+ if ($speed -eq '1x') { $speed } else { $speed + ' slower' }
209
+ }
210
+ } | Format-Table -AutoSize
211
+}
212
+```
213
+
214
+When you run the script in PowerShell 7.4.6, you see that using the `+=` operator is the slowest
215
+method.
216
+
217
+```Output
218
+CollectionSize Test TotalMilliseconds RelativeSpeed
219
+-------------- ---- ----------------- -------------
220
+ 5120 Direct Assignment 4.17 1x
221
+ 5120 List<T>.Add(T) 90.79 21.77x slower
222
+ 5120 Array+= Operator 342.58 82.15x slower
223
+
224
+
225
+CollectionSize Test TotalMilliseconds RelativeSpeed
226
+-------------- ---- ----------------- -------------
227
+ 10240 Direct Assignment 0.64 1x
228
+ 10240 List<T>.Add(T) 184.10 287.66x slower
229
+ 10240 Array+= Operator 1668.13 2606.45x slower
230
+```
231
+
232
+When you run the script in PowerShell 7.5-rc.1, you see that using the `+=` operator is much faster
233
+than PowerShell 7.4.6. Now, it's also faster than using the `List<T>.Add(T)` method.
234
+
235
+```Output
236
+CollectionSize Test TotalMilliseconds RelativeSpeed
237
+-------------- ---- ----------------- -------------
238
+ 5120 Direct Assignment 4.71 1x
239
+ 5120 Array+= Operator 40.42 8.58x slower
240
+ 5120 List<T>.Add(T) 92.17 19.57x slower
241
+
242
+
243
+CollectionSize Test TotalMilliseconds RelativeSpeed
244
+-------------- ---- ----------------- -------------
245
+ 10240 Direct Assignment 1.76 1x
246
+ 10240 Array+= Operator 104.73 59.51x slower
247
+ 10240 List<T>.Add(T) 173.00 98.3x slower
248
+```
153249
154250
<!-- end of content -->
155251
<!-- reference links -->
⋯ 79 unchanged lines
Microsoft's Markdown source from MicrosoftDocs/PowerShell-Docs, © Microsoft Corporation, under CC BY 4.0. Changed lines with up to 3 unchanged lines around each; ChangeIntel kept this copy 10 Oct 17:56 UTC. The commit date is when the source changed, which can be hours or days before Learn published it.