I am refactoring some old(ish) code, and I have used two approaches for creating some arrays and ordered dictionaries, for example...
[Collections.Specialized.OrderedDictionary]@{}
and
New-Object Collections.Specialized.OrderedDictionary
I wonder which of these two approaches to instantiating arrays and hashtables/dictionaries is better? FWIW, I need code that that is version agnostic from PS2.0 thru current. Performance is a secondary concern.
FWIW, it looks like the former is MUCH better from a performance standpoint. I tried
Measure-Command {
foreach ($i in 1..10000) {
$array1 = [Collections.Specialized.OrderedDictionary]@{}
}
}
Measure-Command {
foreach ($i in 1..10000) {
$array2 = New-Object Collections.Specialized.OrderedDictionary
}
}
and got 34 milliseconds vs 278 milliseconds. Of course I am not creating 10K instances, nor is performance the main priority, not is 278 milliseconds poor performance even if that was a priority. But it sure shows there is a big difference in the process, even if the end result is actually the same.