How to interpret Chromium & Firefox performance profiling results of same JavaScript code snippet

蹲街弑〆低调 提交于 2021-02-11 14:03:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!