antoyo
antoyo

Reputation: 11923

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.

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.

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?

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. 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.

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?

Upvotes: 6

Views: 888

Answers (2)

Michael Vogt
Michael Vogt

Reputation: 56

There is also a bug in current versions of firefox that prevent full JIT of javascript in addons, for details see https://bugzilla.mozilla.org/show_bug.cgi?id=913182

Upvotes: 1

paa
paa

Reputation: 5054

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).

Some versions of Firefox ship with the former disabled by default.

Check javascript.options.methodjit.chrome to see if it's set to true.

Upvotes: 1

Related Questions