<iframe id="qiita-embed-content__f34d4d23491f42e1727ff5dc7ebedd28">
</iframe>
VSCode變慢的最常見原因是安裝的擴展功能的數量或質量。其他原因包括:
從命令面板(Ctrl + Shift + P)執行「Developer: Show Running Extensions」,可以查看各擴展功能的啟動時間。
Developer: Show Running Extensions


啟動時間越長,對效能的影響越大。
VSCode具有Extension Bisect功能,可以通過切換擴展功能的啟用狀態,按照二分搜索的方法找出問題擴展功能。
Start Extension Bisect



從命令面板執行「Extensions: Disable All Installed Extensions」,暫時關閉所有擴展功能,只啟用真正需要的功能。
不同的專案需要的擴展功能各不相同。僅在特定工作區啟用擴展功能,可以減少其他專案的資源消耗。
右鍵擴展功能 > 禁用(工作區)

在擴展功能搜尋框中搜索「@builtin」,將顯示預設安裝的擴展功能清單。禁用不使用的功能。

在我的環境中安裝了40個擴展功能,原本所有擴展功能都啟用。
這些擴展功能中有很多是一開始使用卻最終沒有日常使用的,或是因為推薦而暫時安裝的。
在這種情況下啟動VSCode時的效能如下:
(特別影響大的項目TOP5,列出了三次啟動效能的平均值)
| 順位 | 項目 | 持續時間 | 處理程序 |
|---|---|---|---|
| 1 | 擴展功能登記 | 2104 | [渲染器] |
| 2 | 工作區準備 | 1130 | [主程序->渲染器] |
| 3 | 登記擴展功能並啟動擴展主機 | 780 | [渲染器] |
| 4 | 渲染器準備 | 551 | [渲染器] |
| 5 | 整體工作區負載 | 308 | [渲染器] |
接下來我們來看看如果僅啟用真正需要的擴展功能,效能會有什麼變化。
實際使用的擴展功能減少到20個。
| 順位 | 項目 | 持續時間 | 處理程序 |
|---|---|---|---|
| 1 | 擴展功能登記 | 1548 | [渲染器] |
| 2 | 工作區準備 | 961 | [主程序->渲染器] |
| 3 | 登記擴展功能並啟動擴展主機 | 495 | [渲染器] |
| 4 | 渲染器準備 | 511 | [渲染器] |
| 5 | 整體工作區負載 | 270 | [渲染器] |
與原始效能相比↓
| 項目 | 以前的平均 | 最新的平均 | 改善量 | 改善率 |
|---|---|---|---|---|
| 擴展功能登記 | 2104 | 1548 | 556 | 26% |
| 工作區準備 | 1130 | 961 | 169 | 15% |
| 登記擴展功能並啟動擴展主機 | 780 | 495 | 285 | 37% |
| 渲染器準備 | 551 | 511 | 40 | 7% |
| 整體工作區負載 | 308 | 270 | 38 | 12% |
整體的啟動效能有所提升,體感上從啟動到加載時間明顯變快。
像我一樣,堆積了「隨便安裝的」擴展功能的人應該不在少數,建議利用這個機會整理一下。
VSCode為了加快運行速度,檔案監視過程會消耗大量記憶體和CPU。請將不需要監視的目錄排除。
// settings.json
{
"files.watcherExclude": {
"**/.git/objects/**": true,
"**/.git/subtree-cache/**": true,
"**/node_modules/*/**": true,
"**/dist/**": true,
"**/build/**": true,
"**/.vscode/**": true,
"**/coverage/**": true,
"**/__generated__/**": true,
"**/tmp/**": true,
"**/.cache/**": true
}
}
指定不在側邊欄中顯示的檔案和目錄,可以減輕檔案總管的負擔。
{
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/node_modules": true,
"**/__pycache__": true,
"**/.next": true,
"**/dist": true,
"**/build": true
}
}
通過從搜索中排除,可以提高搜索性能。
{
"search.exclude": {
"**/node_modules": true,
"**/bower_components": true,
"**/*.code-search": true,
"**/dist": true,
"**/build": true,
"**/.next": true,
"**/coverage": true
}
}
透過適當指定exclude,可以防止編輯器或構建過程中不必要的文件解析,從而提高性能。
TypeScript預設排除node_modules,但顯式指定也無妨。
// tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "commonjs",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true, // 跳過型別定義文件檢查(加速)
"forceConsistentCasingInFileNames": true
},
"include": [
"src/**/*"
],
"exclude": [
"node_modules",
"dist",
"build",
"**/*.spec.ts",
"**/*.test.ts",
"coverage"
]
}
補充
skipLibCheck: true 對性能改善非常有效。exclude 僅對 include 的結果有影響,因此通過匯入引入的文件可能不會被排除。透過禁用TypeScript伺服器的自動日誌輸出及自動型別獲取,可以使編輯器運行更流暢。
// .vscode/settings.json
{
"typescript.tsserver.log": "off", // 停止tsserver的日誌輸出
"typescript.disableAutomaticTypeAcquisition": true, // 禁用自動型別獲取
"typescript.tsserver.experimental.enableProjectDiagnostics": false // 減少大型專案的診斷負擔
}
效果
刪除TypeScript的內部快取,可能在某些環境下改善記憶體使用量和檔案監視負擔。
主要快取文件夾(可能並不在所有環境中存在)
C:\Users\<使用者名稱>\AppData\Local\Microsoft\TypeScript~/Library/Caches/Microsoft/TypeScript~/.cache/typescript注意
{
"editor.minimap.enabled": false
}
{
"breadcrumbs.enabled": false
}
{
"editor.codeLens": false
}
{
"workbench.activityBar.visible": false,
"window.menuBarVisibility": "toggle"
}
{
"files.autoSave": "onFocusChange",
"files.autoSaveDelay": 1000
}
ESLint自動代碼格式化在專案增大時處理時間會增加,可能會留下殭屍進程。
{
"editor.formatOnSave": true,
"editor.formatOnType": false,
"editor.formatOnPaste": false,
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
}
{
"git.enabled": true,
"git.autorefresh": false,
"git.autofetch": false,
"git.decorations.enabled": false
}
{
"files.maxMemoryForLargeFilesMB": 4096
}
在使用WSL2 + Docker環境的VSCode中,如果出現VmmemWSL大量消耗記憶體的現象,多數是因為檔案監視有問題。請在工作區中應用上述files.watcherExclude設置。
定期清除快取,能使運行變得更加流暢。
Windows的情況
C:\Users\<使用者名稱>\AppData\Local\Code\Cache
C:\Users\<使用者名稱>\AppData\Local\Code\CachedData
macOS的情況
~/Library/Application Support/Code/Cache
~/Library/Application Support/Code/CachedData
注意: 不要刪除User文件夾(設置文件)或extensions(擴展功能)。
以下是為減少記憶體使用量的綜合設置範例↓
{
// 檔案監視的排除
"files.watcherExclude": {
"**/.git/objects/**": true,
"**/.git/subtree-cache/**": true,
"**/node_modules/*/**": true,
"**/dist/**": true,
"**/build/**": true,
"**/.vscode/**": true,
"**/coverage/**": true,
"**/__generated__/**": true
},
// 從檔案總管排除
"files.exclude": {
"**/.git": true,
"**/.DS_Store": true,
"**/node_modules": true,
"**/dist": true,
"**/build": true
},
// 從搜索排除
"search.exclude": {
"**/node_modules": true,
"**/dist": true,
"**/build": true,
"**/.next": true,
"**/coverage": true
},
// TypeScript優化
"typescript.tsserver.log": "off",
"typescript.disableAutomaticTypeAcquisition": true,
// UI輕量化
"editor.minimap.enabled": false,
"breadcrumbs.enabled": false,
"editor.codeLens": false,
// 自動保存和格式
"files.autoSave": "onFocusChange",
"editor.formatOnSave": true,
"editor.formatOnType": false,
"editor.formatOnPaste": false,
// Git設置
"git.autorefresh": false,
"git.autofetch": false,
"git.decorations.enabled": false,
// 其他
"files.maxMemoryForLargeFilesMB": 4096
}
如果想要詳細分析VSCode的性能,請使用以下命令。
命令面板 > Developer: Startup Performance

該命令會分析啟動中花費時間的具體原因。單位是毫秒,Duration是該項目所花的時間。
應用本文中介紹的設置,可以大幅減少VSCode的記憶體使用量。特別是以下三點最為有效:
如果這些設置試過後仍未改善,可以考慮重新安裝VSCode,或作為最終手段轉向VSCode Insiders。
定期清除快取,刪除不需要的擴展功能,可以維持舒適的開發環境。
原文出處:https://qiita.com/nolanlover0527/items/071277263f8851012e6b