記事ディレクトリ
1. オンデマンドでモジュールをロードする
すぐに使用する必要のないモジュールの導入を使用時まで遅らせ、一度にヘッダー ファイルにあまりにも多くの依存関係を導入することを回避し、ページの読み込みが過度に肥大化する (UI コンポーネント ライブラリなど) js プラグイン)
2. ウィンドウは最初に作成され、次に非表示になり、初期化後に表示されます
目隠しのようなもので、根本的に速度を最適化するわけではありません
const options = {
width: 500,
height: 890,
center: true,
title: '桌面开发App',
icon: path.join(__dirname, 'icon.png'),
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
nodeIntegration: true,
// enableRemoteModule:true,
contextIsolation: false,
nodeIntegrationInWorker: true,
enableRemoteModule: true,
webSecurity: false,
// 允许使用webview
// webviewTag: true
},
//优化速度重点在这里
show: false, // newBrowserWindow创建后先隐藏
}
// Create the browser window.
const mainWindow = new BrowserWindow(
options
);
globalShortcut.register('Control+Shift+i', function () {
mainWindow.webContents.openDevTools()
})
mainWindow.on('focus', () => {
globalShortcut.register('CommandOrControl+F', function () {
if (mainWindow && mainWindow.webContents) {
mainWindow.webContents.send('on-find', '')
}
})
})
mainWindow.on('blur', () => {
globalShortcut.unregister('CommandOrControl+F')
})
//优化速度重点在这里
mainWindow.on('ready-to-show', () => {
mainWindow.show() // 初始化后再显示
})
3. Web パフォーマンスの最適化
Electron クライアントは h5 ページに基づいて開発されており、起動を高速化するエレガントで簡潔な高性能コードを備えています。
4. メインプロセス (electron.js) を最適化する
node.js サブプロセスを使用して、メイン プロセスの負担を最適化します。スレッド処理用に新しい Worker を作成することもできますが、ページ DOM と対話することはできません。頻繁なユーザー操作やキャッシュ比較などのシナリオに適しています。 .