JSFiddle - React, Tailwind, and code Playground
HTML
<html>
<body>
<div id="parent">
</div>
</body>
</html>
JavaScript
const numberOfSiblings = 100000; //Number of divs to append to parent
const startingElement = "div-3000"; //id of element we want to pass into functions
const strideNum = 2000; //Number of siblings away from the chosen starting point
function strideSiblings(element, stride){
var parent = element.parentNode;
var index = Array.prototype.indexOf.call(parent.children, element);
return parent.children[index + stride];
}
function strideSiblingsWithPreviousOrNext(element, stride){
let sib = null;
if(stride > 0){
for(let i = 0; i < stride; i++){
sib = element.nextSibling;
element = sib;
}
}else{
stride = stride * -1;
for(let j = 0; j < stride; j++){
sib = element.previousSibling;
element = sib;
}
}
return sib;
}
let par = document.getElementById("parent");
for(let v = 0; v < numberOfSiblings; v++){
let newDiv = document.createElement("div");
newDiv.id = "div-" + v;
newDiv.textContent = "Here is div-" + v;
par.append(newDiv);
}
var el = document.getElementById(startingElement);
var t0 = performance.now()
var stridedSibling = strideSiblings(el, strideNum);
var t1 = performance.now()
console.log("Call to strideSiblings() took " + (t1 - t0) + " milliseconds.");
var t2 = performance.now();
stridedSibling = strideSiblingsWithPreviousOrNext(el, strideNum);
var t3 = performance.now();
console.log("Call to strideSiblingsWithPreviousOrNext() took " + (t3 - t2) + " milliseconds.");
//console.log(stridedSibling);