Javascript get element child index

5:36 AM 1/27/2019 - 4:02 PM 1/29/2019

This is just a test of some works, worthy to take note of, from:

The test

Please wait... generating 100,000 element child elements:
time = 100 ms Sorry, your browser doesn't support HTML5 <canvas> technology. time = 0 ms,index = 0 index = 100,000

Some test results

with Firefox 64 for Windows 7, on 32-bit machine:

with Chrome 71 for Windows 7, on 32-bit machine:

with Internet Explorer 11 for Windows 7, on 32-bit machine:

with Opera 58 for Windows 7, on 32-bit machine:

Conclusion

We surely don't want the worst: green plots (appeared on Firefox, Chrome and Opera). They represent the Array.prototype.indexOf.call(sel.parentNode.children, sel) method. On Internet Explorer however, the worst appears to be the yellow plot which represents the while(j.previousElementSibling){j = j.previousElementSibling; i++;}. So this leaves us the red and blue plots.

As blue seems to be better always than red in all tested browsers, it becomes my preferred choice for getting element child index: for(i = 0; (j = j.previousElementSibling); i++);.

I wonder a bit how this all would look... on 64-bit machines?

Comments