Skip to content

Does loading a webextension’s background.js script impact performance, even if the script’s contents are minimal?

When installing a webextension, does the mere existence of a background.js file negatively impact performance* at all, considering that it is a separate javascript file that must be loaded?

For most practical cases one could assume the answer is “pretty much No,” but in the case of an older computer with less processing power, and a browser that could have dozens to hundreds of extensions, is it possible the cumulative strain of loading all those scripts in the background could produce a sizable impact, even if the contents of the scripts are not particularly resource-heavy?

From a CS standpoint does the very act of loading the javascript file impact performance, or is it only when code inside the file is executed that system resources are used?

In other words, to choose the most lightweight webextension possible, should one avoid background.js files, or does it not matter at all?

*in terms of CPU usage, RAM usage, browser startup time, or page load time

Answer

The answer to this question can be yes, no, and maybe, depending on what specifically those extensions do in their background scripts, and what the user does. Some negatives are always present though.

When the browser starts:

  • When the browser starts it reads all the extensions and every file inside to verify the hashsum (CRC) so just this will be slow, especially on a non-SSD drive.
  • Creating a new JS environment for each background script also takes time, roughly 50ms or 20 extensions per second on one CPU thread/core.

When the browser already runs:

  • If an extension’s background script is declared as non-persistent, it will load it each time the popup is shown or a registered API event is fired, then it’ll be unloaded.
  • If the background script is persistent (it always is in Firefox), which is the default mode when the author doesn’t redefine it, the background script won’t unload when the extension is unused and will consume 10-20 MB or sometimes much more if the extension has a bug or there’s a bug in the browser. Hundreds of extensions will consume gigabytes of RAM which reduces the amount of available memory for your web pages and other apps.
  • The background script of each extension runs in its extension’s process allocated by the OS, but browsers try to limit the number of system processes they use to avoid crashing the OS, so when there are many extensions the browser will reduce the amount of processes it uses to isolate normal web sites and if there are many tabs with different sites it’ll start grouping unrelated sites in one system process, thus reducing their resistance against side-channel attacks like Spectre. Depending on the browser and its version, the extensions may be also grouped in one system process, which is even worse as they are allowed to use various secure API that can manage user data so a successful side-channel attack is more damaging.