From 872d6b0bd559f38a03fc948501e5b6d7e3084879 Mon Sep 17 00:00:00 2001 From: EvilGorilla13 <66751550+EvilGorilla13@users.noreply.github.com> Date: Fri, 2 Oct 2026 18:48:15 -0400 Subject: [PATCH] Optimize installed app detection (#771) --- Scripts/FileIO/Import-AppDetailsFromJson.ps1 | 13 +++++++++++-- Tests/Settings-And-Apps-FileIO.Tests.ps1 | 3 ++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/Scripts/FileIO/Import-AppDetailsFromJson.ps1 b/Scripts/FileIO/Import-AppDetailsFromJson.ps1 index c7fad69..684adeb 100644 --- a/Scripts/FileIO/Import-AppDetailsFromJson.ps1 +++ b/Scripts/FileIO/Import-AppDetailsFromJson.ps1 @@ -37,6 +37,16 @@ function Import-AppDetailsFromJson { return $apps } + $installedAppNames = @{} + if ($OnlyInstalled) { + # Get-AppxPackage is expensive; query it once and use its package names for the catalog lookup. + foreach ($installedApp in @(Get-AppxPackage -ErrorAction SilentlyContinue)) { + if ($installedApp.Name -is [string]) { + $installedAppNames[$installedApp.Name] = $true + } + } + } + foreach ($appData in $jsonContent.Apps) { # Handle AppId as array (could be single or multiple IDs) $appIdArray = @( @@ -51,8 +61,7 @@ function Import-AppDetailsFromJson { if ($OnlyInstalled) { $isInstalled = $false foreach ($appId in $appIdArray) { - # Check Get-AppxPackage first (fast, no process launch) - if (Get-AppxPackage -Name $appId) { + if ($installedAppNames.ContainsKey($appId)) { $isInstalled = $true break } diff --git a/Tests/Settings-And-Apps-FileIO.Tests.ps1 b/Tests/Settings-And-Apps-FileIO.Tests.ps1 index 54ba1e9..abcfd9a 100644 --- a/Tests/Settings-And-Apps-FileIO.Tests.ps1 +++ b/Tests/Settings-And-Apps-FileIO.Tests.ps1 @@ -187,13 +187,14 @@ Describe 'Import-AppDetailsFromJson' { } It 'filters to installed apps using Appx and winget detection' { - Mock Get-AppxPackage { param($Name) if ($Name -eq 'Two.App') { [PSCustomObject]@{ Name = $Name } } } + Mock Get-AppxPackage { [PSCustomObject]@{ Name = 'Two.App' } } Mock Test-AppInWingetList { $false } $apps = @(Import-AppDetailsFromJson -OnlyInstalled -InstalledList @()) $apps | Should -HaveCount 1 $apps[0].AppId | Should -Be 'Two.App' + Should -Invoke Get-AppxPackage -Times 1 -Exactly } It 'loads presets and preserves their ID arrays' {