<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
//Add 100,000 elements
var frag = document.createDocumentFragment();
for (var i=0; i<10; i++){
var outSection = document.createElement('section');
outSection.id="secid_"+i;
for (var j=0; j<100; j++){
var midDiv = document.createElement('div');
midDiv.id="mdivid_"+j;
for (var k=0; k<100; k++){
var inDiv = document.createElement('div');
inDiv.id="divid_"+k;
midDiv.appendChild(inDiv);
}
outSection.appendChild(midDiv)
}
frag.appendChild(outSection);
}
document.body.appendChild(frag);
var a = 0;
$('section').each(function () {
a++;
});
function forEachElement(selector, fn) {
var elements = document.getElementsByTagName(selector);
for (var i = 0; i < elements.length; i++) fn(elements[i]);
}
var a = 0;
forEachElement('section', function (el){
a++;
});
var a = 0;
$('section').each(function () {
a++;
});
function forEachElement(selector, fn) {
var elements = document.getElementsByTagName(selector);
for (var i = 0; i < elements.length; i++) fn(elements[i]);
}
var a = 0;
forEachElement('div', function (el){
a++;
});
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery | |
pure JS | |
jQuery DIV | |
pure JS DIV |
Test name | Executions per second |
---|---|
jQuery | 698267.9 Ops/sec |
pure JS | 1376254.8 Ops/sec |
jQuery DIV | 705550.1 Ops/sec |
pure JS DIV | 175.9 Ops/sec |
Let's dive into the provided benchmark data.
Benchmark Overview
The benchmark compares two approaches for iterating over HTML elements:
each
methodforEachElement
functionOptions Compared
The options being compared are:
each
method to iterate over elements with a specific tag name (section
)forEachElement
function to iterate over elements with a specific tag name (section
)Pros and Cons of Each Approach
jQuery's each
Method:
Pros:
Cons:
Pure JavaScript Implementation
Pros:
Cons:
Library: jQuery
jQuery is a popular JavaScript library that provides a set of pre-built functions for DOM manipulation and event handling. The each
method is part of this library and is used to iterate over elements with a specific tag name.
In the context of this benchmark, jQuery's each
method is used to iterate over section
elements, while the pure JavaScript implementation uses its own custom function (forEachElement
) for similar purposes.
Special JS Feature: forEachElement
The forEachElement
function is a custom implementation introduced in the benchmark. It takes two arguments: selector
and fn
. The function iterates over all elements with the specified selector
(in this case, section
) and applies the provided fn
callback to each element.
This feature allows developers to write more flexible iteration logic without relying on jQuery's each
method. However, it also adds an extra layer of complexity and requires manual management of loops.
Other Considerations
When choosing between these two approaches, consider the following factors:
each
method can be easier to write and understand for simple iterations.Alternatives
Other alternatives for iterating over elements in JavaScript include:
forEach
method on the NodeList
interface (available in modern browsers)requestAnimationFrame
or Web Workers
for optimized performanceKeep in mind that each approach has its strengths and weaknesses, and the best choice depends on the specific use case and requirements.