问题
While being a web developer for over 3 years, I now realize that I know nothing about browsers and that they can differ in many aspects from each other as day & night. As an illustration to this,- I've tried to construct somewhat a "fair-test" for profiling a JavaScript parity check code, which version of it runs faster, this x => (x%2)==0
or that x => (x&1)==0
.
I suspect that checking only right-most bit, should be faster way, but in a browser world ... you may never know. Here's a complete JavaScript test code :
function evaluateSpeed(func) {
const times = 100000000;
const salt = 96145;
let check = salt;
for (let i = 0; i < times; i++) {
check ^= salt + (i&0xff)<<(1+func(i));
}
return check;
}
function evenStd() {
return evaluateSpeed(x => (x%2)==0);
}
function evenEff() {
return evaluateSpeed(x => (x&1)==0);
}
function test() {
let y1 = evenStd();
let y2 = evenEff();
if (y1 !== y2) {
throw new Error('Semantic error !');
}
else {
document.write("Test is done !");
}
}
test();
And here is the profiling results in a Chromium web browser :
To my surprise function evenEff()
(bit-checking function) runs 15x slower than evenStd()
(modulus division by 2 function) in Chromium ! Here's a profiling results of same code in a Firefox browser :
In a Firefox I see expected results, that evenEff()
runs about 2x faster than alternative version evenStd()
. Here comes the question, What's going on under the hood of a Chromium web browser? Why such weird results? Is it related to code caching? Maybe the remainder operator is better offloaded to GPU cores in Chromium case or something? Or maybe just test isn't fair enough and not cross-browser compatible?
来源:https://stackoverflow.com/questions/65029918/how-to-interpret-chromium-firefox-performance-profiling-results-of-same-javasc