PoshCode / Configuration

A module to help other modules have settings
MIT License
176 stars 27 forks source link

Use square bracket indexing for better performance #39

Closed ajhall closed 3 years ago

ajhall commented 3 years ago

Use square bracket indexing for better performance in ConvertTo-Metadata.ps1.

We noticed over in https://github.com/devblackops/Terminal-Icons/issues/13 that large and complex psd1 files took a few seconds to import, which is a problem for modules that get auto-loaded in the user profile. I was able to narrow it down to the ConvertTo-Metadata function, and specifically the part of it that handles dictionaries. For whatever reason, using $InputObject[$key] is quite a bit faster than $InputObject.($key) while providing the same result.

I included a (very rough) benchmark to prove the difference in performance. It's a bit janky - I dot-source everything in Source\Metadata to get the functions in scope before running the benchmark, and it triggers an error from Export-ModuleMember that can be safely ignored. Let me know if you'd like me to remove it from the pull request or what the proper way would be to handle this instead.

On my machine, it runs in about 350 milliseconds before the change and about 75 milliseconds after the change.

Jaykul commented 3 years ago

I don't doubt you. I haven't come back and applied that lesson to this module, but I did learn the same performance truth...

Jaykul commented 3 years ago

I'm going to merge this by rebasing onto master and do a new PR from ajhall/fix/performance just in case you need your branch. See #41