<div class="container"></div>
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 *');
for (let el of elements) {
const zIndex = Number(el.style.zIndex);
if (!isNaN(zIndex)) {
console.log(true);
}
}
for (let el of elements) {
const zIndex = Number.parseInt(el.style.zIndex, 10);
if (Number.isInteger(zIndex)) {
console.log(true);
}
}
for (let el of elements) {
const zIndex = Number.parseInt(el.style.zIndex, 10);
if (zIndex || zIndex === 0) {
console.log(true);
}
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Number + isNaN | |
Number.parseInt + Number.isInteger | |
Number.parseInt + simple condition |
Test name | Executions per second |
---|---|
Number + isNaN | 3990.1 Ops/sec |
Number.parseInt + Number.isInteger | 41885.5 Ops/sec |
Number.parseInt + simple condition | 53402.2 Ops/sec |
Let's dive into the benchmark and explain what's being tested.
Benchmark Overview
The benchmark is designed to test the performance of different approaches for parsing and checking the value of zIndex
style property in HTML elements using JavaScript.
Test Cases
There are three individual test cases:
zIndex
value can be successfully parsed as a number using the built-in Number()
function and then checked for NaN (Not a Number) using the isNaN()
function.Number.parseInt()
function to parse the zIndex
value with a radix of 10, and then checks if the parsed value is an integer using the Number.isInteger()
function.Number.parseInt()
function to parse the zIndex
value with a radix of 10, and then applies a simple conditional check: if the parsed value is truthy (i.e., not zero or NaN).Library Usage
None of the test cases use any external libraries.
JavaScript Features and Syntax
None of the test cases explicitly use special JavaScript features or syntax.
Benchmark Results
The latest benchmark results show that:
Other Considerations
When choosing a parsing and checking approach for zIndex
values, developers should consider the following factors:
Number.parseInt()
with a radix of 10 is a good option if you want to ensure that the parsed value is an integer. However, this may be slower than using Number()
alone.Alternatives
If you're looking for alternative approaches or libraries, consider the following:
zIndex
values, but this may be slower than using parsing functions.Number.parseInt()
for simple cases.Keep in mind that this benchmark is specific to the tested use case, and you should adapt your approach based on your project's requirements.