forEach vs querySelector (v7)

Revision 7 of this benchmark created on


Preparation HTML

<div class="root">
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div class="payments-container">
        <div data-js-payment-content=""></div>
        <div class="h-hidden" data-js-payment-content=""></div>
        <div data-js-payment-content=""></div>
    </div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</div>

Setup

const root = document.querySelector('.root');
const paymentContents = document.querySelectorAll('[data-js-payment-content]');
const hiddenClass = 'h-hidden';
const paymentsContainer = document.querySelector('.payments-container');

Test runner

Ready to run.

Testing in
TestOps/sec
forEach
let aPaymentMethodIsOpen = false;
paymentContents.forEach(element => {
            if (!element.classList.contains(hiddenClass)) {
                aPaymentMethodIsOpen = true;
            }
        });
ready
querySelector en root
const aPaymentMethodIsOpen = root.querySelector(`div[data-js-payment-content]:not(.${hiddenClass})`) !== null;
ready
querySelector en parent de los paymentContents
const aPaymentMethodIsOpen = paymentsContainer.querySelector(`div[data-js-payment-content]:not(.${hiddenClass})`) !== null;
ready
Con Array.prototype
const aPaymentMethodIsOpen = Array.prototype.some.call(paymentsContainer, (element) => !element.classList.contains(hiddenClass))
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.