Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.4 Safari/605.1.15
Safari 17
Mac OS X 10.15.7
Desktop
11 months ago
Test name Executions per second
Number + isNaN 7131.7 Ops/sec
Number.parseInt + Number.isInteger 28549.5 Ops/sec
Number.parseInt + simple condition 38879.5 Ops/sec
HTML Preparation code:
AخA
 
1
<div class="container"></div>
Script Preparation code:
x
 
const container = document.querySelector('.container');
const cloneTarget = document.createElement('div');
const listToInsert = document.createElement('div');
listToInsert.id = 'clone-list';
for (let i = 0; i < 100; i++) {
    const clone = cloneTarget.cloneNode(true);
    clone.id = "clone_" + i;
    clone.textContent = 'I am clone ' + i;
    listToInsert.append(clone);
}
container.append(listToInsert);
var elements = document.querySelectorAll('#clone-list *');
Tests:
  • Number + isNaN

     
    for (let el of elements) {
        const zIndex = Number(el.style.zIndex);
        if (!isNaN(zIndex)) {
            console.log(true);
        }
    }
  • Number.parseInt + Number.isInteger

     
    for (let el of elements) {
        const zIndex = Number.parseInt(el.style.zIndex, 10);
        if (Number.isInteger(zIndex)) {
            console.log(true);
        }
    }
  • Number.parseInt + simple condition

     
    for (let el of elements) {
        const zIndex = Number.parseInt(el.style.zIndex, 10);
        if (zIndex || zIndex === 0) {
            console.log(true);
        }
    }