简体   繁体   English

为什么FireFox插件中的JavaScript比FireFox加载的网页中的JavaScript慢?

[英]Why is JavaScript slower in a FireFox add-on than in a webpage loaded in FireFox?

I wonder why the same JavaScript code is considerably slower in a FireFox add-on (using the Add-on SDK) than directly running in a web page loaded in FireFox. 我想知道为什么在FireFox附加组件(使用附加组件SDK)中使用相同的JavaScript代码要比直接在FireFox中加载的网页中运行慢得多。

For instance, this code: 例如,此代码:

function isPrime(number) {
    var i,
        prime = true;
    for(i = 2 ; i < number ; ++i) {
        if(number % i === 0) {
            prime = false;
        }
    }
    return prime;
}

function sumFirstPrimeNumbers(x) {
    var i,
        sum = 0;
    for(i = 1 ; i <= x ; ++i) {
        if(isPrime(i)) {
            sum += i;
        }
    }
    return sum;
}

var sum = sumFirstPrimeNumbers(15000);

console.log(sum);

takes less than 2 seconds to run in a webpage opened in FireFox, but takes about 15 seconds to run in a FireFox add-on. 在FireFox中打开的网页中运行所需的时间少于2秒,而在FireFox附加组件中运行所需的时间约为15秒。

I know the code could be better, but it is only an example to show how slow it is. 我知道代码可能会更好,但是这只是说明它有多慢的一个例子。

Why is it that slow in a FireFox add-on? 为什么在FireFox插件中这么慢?

Is there any way to get it faster (without changing this code since it is, as I said above, only an example)? 有什么方法可以使它更快(不更改此代码,因为如上所述,它只是一个示例)?

Update: 更新:

It seems to be related to the Add-on SDK. 它似乎与附加SDK有关。 I did another test: I executed the same code in an add-on which does not use the add-on SDK and the code execute in about 3 seconds. 我做了另一个测试:我在不使用附加SDK的附加组件中执行了相同的代码,并且该代码在大约3秒钟内执行。

Why such a huge difference (3 seconds vs 15 seconds) between an add-on using the add-on SDK and an add-on not using it? 为什么使用附加SDK的附加组件与不使用附加SDK的附加组件之间会有如此巨大的差异(3秒vs 15秒)?

There are two preferences (accessible from the about:config page) that control the javascript optimizations: javascript.options.methodjit.chrome for privileged code (extensions) and javascript.options.methodjit.content for untrusted code (web pages). 有两个偏好(从访问about:config页)控制的JavaScript优化: javascript.options.methodjit.chrome的特权代码(扩展)和javascript.options.methodjit.content不受信任的代码(网页)。

Some versions of Firefox ship with the former disabled by default. 某些版本的Firefox默认情况下禁用前者。

Check javascript.options.methodjit.chrome to see if it's set to true . 检查javascript.options.methodjit.chrome以查看是否将其设置为true

当前版本的firefox中还存在一个错误,该错误会阻止插件中的javascript完全JIT,有关详细信息,请参见https://bugzilla.mozilla.org/show_bug.cgi?id=913182

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM