why Firefox runs this code 10x faster than Chrome
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