Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why Firefox runs this code 10x faster than Chrome

I need to run this code on WebKit, it's a part of a hybrid app for android:

for(var x = 0; x < width; x++) {
    for(var y = 0; y < height; y++) {
        var i = (y * width + x) * 3;
        var r = data[i];
        var g = data[i + 1];
        var b = data[i + 2];
        var green = is_green(r, g, b);
        x_histogram[x] += green;
        y_histogram[y] += green;
    }
}

Here is full code to test: https://jsbin.com/boduputebu/edit?js,console

I thought V8 is faster than Firefox (SpiderMonkey), but here for this simple code SpiderMonkey is significantly faster. On my laptop the performance is:

Chrome: 30 ms
Node: 30 ms
Firefox: 3 ms
Java (same code with Java): 3 ms

Do you have any idea to change the code to make it fast on V8. With current performance I had to write it native on Java side, but it's not a good option for me. Or if there is no way to make it faster do you know why V8 runs this code very slower?

Version:

Chrome: "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.106 Safari/537.36"
FireFox: "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:49.0) Gecko/20100101 Firefox/49.0"
like image 518
iman Avatar asked Sep 26 '16 11:09

iman


People also ask

Is Firefox slower than Chrome?

Mozilla claims that Firefox loads websites slightly faster than Chrome. Firefox does seem snappier sometimes, but not always. Here's the same site loaded on Firefox: The page loads slightly faster than on Chrome — the image shows almost instantaneously, while Chrome takes a few extra milliseconds.

What is better Firefox or Chrome?

Is Firefox Really Better Than Chrome? Firefox is a more private and secure browser than Chrome, but Chrome is faster and contains more features. Is Firefox Safer Than Chrome? Both browsers are safe, but Firefox's tracking protection is more comprehensive than Chrome's.

What is the difference between Firefox and Chrome?

Firefox is a free and open-source web browser that provides a number of features for easy web browsing. Google Chrome is a free web browser but it is not open source, it is the most used web browser for desktop.

Is V8 faster than SpiderMonkey?

V8 is the fastest, because it compiles all JS to machine code. SpiderMonkey (what FF uses) is fast too, but compiles to an intermediate byte-code, not machine code. That's the major difference with V8.

Is Mozilla developer really 10x faster than Firefox?

Recently, somewhere, I saw download link saying something like "Mozilla Developer, for people like you", or something alike, I installed it and man, that browser is like 10x faster than regular Firefox AND Chrome AND Safari, I mean, this speed is outrageous! It's great, no sarcasm!

Is Google Chrome really faster than Firefox?

As part of the study, the researchers rounded up 1,495 participants, many of which arrived with the preconceived notion that Chrome is faster than Firefox, rather than the other way around (nearly 39 percent versus 30.5 percent).

Should I use chrome or Firefox or edge for browsing?

I regularly use both Chrome and Firefox for my browsing needs, and on occasion, Edge as well. Without benchmarking the three, I'd be hard pressed to say which one is truly the fastest—it feels like page loads are about the same, for the most part. It may not matter, though.

What is the difference between Firefox and Firefox Developer Edition?

Firefox Developer Edition is a pre-release version of Firefox. So what you see in Developer edition is what will be released to regular Firefox in about 12 weeks (we do releases every 6 weeks, and Dev edition is two releases ahead of release) Dev Edition also uses a fresh profile, which can be faster than your old profile that is full of old data.


2 Answers

This quick n dirty code is already significantly faster in v8. (~24ms for 1000x1000 dataset)

var calc_histogram = function() {
    for(var x = 0; x < width|0; x++) {
        for(var y = 0; y < height|0; y++) {
            var  i = ((y * width + x) * 3)|0;
            var  r = data[i]|0;
            var  g = data[i + 1]|0;
            var  b = data[i + 2]|0;
            var  green = ((g > 80) && (g > (r + 35)|0) && (g > (b + 35)|0))|0;
            x_histogram[x] += green|0;
            y_histogram[y] += green|0;
        }
    }
};

|0 ensure that the number is an integer, it is asm js technique. Calling an array with a number require to make sure it is an integer, using |0 makes it explicit.

EDIT : And this is the fastest I manage to get without unnecessary |0. ~4ms for 500x500 and ~11 for 1000x1000. Note that I inverted the loops so it reads data in sequence to take advantage of prefetch, and I also used a bigger dataset to make improvements noticeable.

var calc_histogram = function() {
    var i=0;
    for(var y = 0; y < height; y++) {
      for(var x = 0; x < width; x++) {
            var r = (data[i|0]+35)|0;
            var g = data[(i+1)|0];
            var b = (data[(i+2)|0]+35)|0;

            if((g > 80) && (g > r) && (g > b)){
              x_histogram[x]++;
              y_histogram[y]++;
            }
            i=(i+3)|0;
        }
    }
}
like image 70
bokan Avatar answered Oct 14 '22 01:10

bokan


I'm from 2021. I am currently using Version 87.0.4280.88 for Chrome and 84.0.2 for Firefox on an i3 Quad Core CPU 64 bit system.

I tried your code and the result is this for Chrome:

enter image description here

And this for FireFox:

enter image description here

So yeah, as of right now the speed results are pretty similar. But as a bonus I made some test code:

    console.time("speed test");
    for(let i = 0; i < 100000; i++) {
      console.log(i);
    }
    console.timeEnd("speed test");

The results are pretty surprising.

Chrome: 2527.755859375 ms

FireFox: 15687ms

like image 29
destroyer22719 Avatar answered Oct 13 '22 23:10

destroyer22719