javascript - why Firefox runs this code 10x faster than Chrome - Stack Overflow

I need to run this code on WebKit, it's a part of a hybrid app for android:for(var x = 0; x < w

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: ,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"

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./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"
Share Improve this question edited Sep 26, 2016 at 11:33 iman asked Sep 26, 2016 at 11:22 imaniman 22.4k9 gold badges34 silver badges32 bronze badges 5
  • While the difference is smaller (8-9 vs 2.5-3 ms), I can reproduce this. Interesting. – Cerbrus Commented Sep 26, 2016 at 11:28
  • 3 FF is not busy to collect your meta data as much as Chrome? – Teemu Commented Sep 26, 2016 at 11:35
  • Do you want a technical explanation about the JavaScript engines internals or just a fix to your code? – Álvaro González Commented Sep 26, 2016 at 11:35
  • 1 x_histogram[x] += green; y_histogram[y] += green; is very costly it seems – Caramiriel Commented Sep 26, 2016 at 11:37
  • 1 @ÁlvaroGonzález, both, I want to fix my code, but also interested to know. – iman Commented Sep 26, 2016 at 11:58
Add a ment  | 

2 Answers 2

Reset to default 4

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;
        }
    }
}

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:

And this for FireFox:

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744234590a4564412.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信