問題描述
我花了幾個小時玩 Electron,我觀察到在屏幕上繪制一個簡單的 html 文件始終需要超過 2.5 秒.時間線大致如下:
I have spent some hours playing with Electron and I have observed that it consistently takes more than 2.5 seconds to draw a trivial html file to the screen. The timeline is roughly as follows:
- 60 毫秒:app
ready
事件被觸發(fā);我們使用new BrowserWindow()
創(chuàng)建一個窗口 - 170 毫秒:屏幕上出現(xiàn)一個空白窗口
- 2800 毫秒:窗口顯示指定的 HTML
- 60 ms: app
ready
event is triggered; we create a window usingnew BrowserWindow()
- 170 ms: a blank window appears on the screen
- 2800 ms: the window shows the specified HTML
我已經(jīng)用我的代碼建立了一個 存儲庫,該代碼源自 Electron 的 快速開始 docs.
I have set up a repository with my code, which is derived from Electron's quick start docs.
關于我的機器,我在 2016 年的 ThinkPad T460 上運行 Windows 10,配備 SSD 和足夠的內(nèi)存.
Regarding my machine, I am running Windows 10 on a ThinkPad T460 from 2016 with a SSD and enough memory.
發(fā)送一個在啟動時顯示一個空白窗口的應用程序?qū)ξ襾碚f是不行的.我假設大多數(shù)開發(fā) Electron 應用程序的人都有類似的想法.因此我的第一個問題是:我做錯了什么嗎?或者這是一個普通 Electron 應用程序的預期加載時間?
Shipping an application that shows a blank window for so long upon startup is a no-go for me. I assume most people developing Electron apps think similarly. Hence my first question: am I doing something wrong? Or is this the expected loading time for a trivial Electron app?
假設這是正常行為,處理此問題的常用方法是什么?想到了一些想法:
Assuming this is normal behavior, what is the common way to deal with this problem? Some ideas come to mind:
- 要求 Electron 顯示啟動畫面:除非有特定的內(nèi)置功能,否則似乎不行,因為啟動畫面本身只會在 2.5 秒后顯示.
- 隱藏應用程序的窗口直到它被渲染(使用
ready-to-show
事件),所以不會顯示空白窗口.這并不理想,因為這意味著用戶不會得到任何關于應用程序?qū)嶋H加載的反饋. - 創(chuàng)建一個包裝應用程序(使用本機代碼),該應用程序顯示啟動屏幕、啟動電子并在顯示電子窗口后隱藏自身.一開始就有點違背了使用 Electron 的目的,因為您最終會編寫本機代碼并意外增加復雜性.
- 按照 文檔.這看起來不太好.
- Asking Electron to show a splash screen: unless there is specific built-in functionality for this, it seems like a no-go, since the splash screen itself would be shown only after 2.5 seconds.
- Hide the app's window until it is rendered (using the
ready-to-show
event), so no blank window is shown. This isn't ideal, since it means that the user doesn't get any feedback whatsoever that the application is actually loading. - Create a wrapper application (using native code) that displays a splash screen, launches electron and hides itself once the electron window is shown. Kind of defeats the purpose of using Electron in the first place, because you end up writing native code and adding accidental complexity.
- Setting the background color of the window to something resembling your app, as suggested by the docs. This just doesn't look very well.
鑒于這一定是一個普遍的問題,我希望社區(qū)已經(jīng)找到了標準的解決方案.如果有人能指出我正確的方向,我會很高興.
Given this must be a common problem, I hope standard solutions have been found by the community. I'd be glad if someone can point me in the right direction.
推薦答案
簡答
Windows Defender 導致速度變慢,因此這不是 Electron 問題.
Short answer
Windows Defender is causing the slowdown, so this is not an Electron problem.
事實證明,Windows Defender 實時保護導致啟動持續(xù)時間比所需時間長得多.關閉實時保護后,我們獲得了可以接受的性能:
It turns out that Windows Defender real-time protection causes startup to last much longer than needed. After turning real-time protection off, we achieved acceptable performance:
- 55 毫秒:應用就緒
- 150 毫秒:顯示空白窗口
- 500 毫秒:加載并顯示 HTML
這意味著我提出的解決方案中的選項 1(顯示啟動屏幕)對于加載緩慢的應用應該非常有效.
This means that option 1 of my proposed solutions (showing a splash screen) should work quite well for slow-loading apps.
剩下的唯一事情就是弄清楚如何解決 Windows Defender 問題.為此,我提出了一個新問題.
The only thing left is to figure out how to solve the Windows Defender problem. For that purpose, I have asked a new question.
這篇關于處理緩慢的電子啟動的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!