Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
for vs for..of vs NodeList.forEach vs Array.forEach vs jQuery.each
(version: 0)
Comparing performance of:
for vs for..of vs NodeList.forEach vs Array.forEach vs jQuery.each
Created:
7 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<content id="demo">Hello World!</content> <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
Script Preparation code:
let content = ''; for (let i=0; i<10000; i+=1) { content += '<div class="component" data-component="component' + i + '">Content ' + i + '</div>'; } document.getElementById("demo").innerHTML = content;
Tests:
for
let components = []; let values = []; const queryComponentsFor = document.querySelectorAll('[data-component]'); for (let i = 0; i < queryComponentsFor.length; ++i) { values.push(queryComponentsFor[i].getAttribute('data-component')); components.push(queryComponentsFor[i]); }
for..of
let components = []; let values = []; for (let el of document.querySelectorAll('[data-component]')) { values.push(el.getAttribute('data-component')); components.push(el); }
NodeList.forEach
let components = []; let values = []; document.querySelectorAll('[data-component]').forEach(el => { values.push(el.getAttribute('data-component')); components.push(el); });
Array.forEach
let components = []; let values = []; [...document.querySelectorAll('[data-component]')].forEach(el => { values.push(el.getAttribute('data-component')); components.push(el); });
jQuery.each
let components = []; let values = []; $('[data-component]').each((i, el) => { values.push(el.getAttribute('data-component')); components.push(el); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
for
for..of
NodeList.forEach
Array.forEach
jQuery.each
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for
359.3 Ops/sec
for..of
242.4 Ops/sec
NodeList.forEach
253.0 Ops/sec
Array.forEach
212.9 Ops/sec
jQuery.each
291.1 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, comparing different approaches for various tasks. In this case, we're analyzing a benchmark that tests four different methods for iterating over an array of HTML elements: `for`, `for..of`, `NodeList.forEach`, `Array.forEach`, and `jQuery.each`. **What is being tested?** The benchmark is testing the performance of each iteration method on an array of 10,000 HTML elements. The test case creates an array of elements using `document.querySelectorAll('[data-component]')` and then iterates over it using one of the four methods. **Options compared** Here's a brief overview of each option being tested: 1. **For loop**: A traditional for loop that uses a counter variable to iterate over the array. 2. **For..of loop**: A newer for loop syntax that allows iterating directly over an iterable (in this case, an array). 3. **NodeList.forEach**: A method on `NodeList` objects that iterates over the elements in the list. 4. **Array.forEach**: A method on arrays that iterates over the elements in the array. 5. **jQuery.each**: A method in jQuery that iterates over a collection of elements. **Pros and Cons** Here's a brief summary of the pros and cons of each option: 1. **For loop**: * Pros: Simple, easy to understand, and widely supported. * Cons: Can be slow for large arrays due to the overhead of the loop variable. 2. **For..of loop**: * Pros: More expressive and concise than traditional for loops, with fewer errors. * Cons: Less widely supported in older browsers, and may require more setup. 3. **NodeList.forEach**: * Pros: Optimized for performance and memory usage, with good support across browsers. * Cons: May not be suitable for arrays, as it's designed specifically for lists. 4. **Array.forEach**: * Pros: Simple and easy to use, with good support across browsers. * Cons: May have slower performance compared to other options due to the overhead of the method call. 5. **jQuery.each**: * Pros: Convenient and easy to use, especially in jQuery-heavy codebases. * Cons: May add unnecessary overhead due to the presence of jQuery. **Other considerations** When choosing an iteration method, consider factors like: * Performance requirements * Code readability and maintainability * Browser support and compatibility In this benchmark, `NodeList.forEach` appears to be the fastest option, followed closely by the for loop. However, the results may vary depending on specific use cases and requirements. **Alternatives** If you're looking for alternatives or want to explore other iteration methods, consider: 1. **Arrow functions**: A concise way to create small, one-time-use functions. 2. **Map(), Filter(), Reduce()**: Methods on arrays that perform transformations, filtering, and aggregation operations. 3. **Generators**: A way to create iterators that can be used in loops or with `for...of`. 4. **Async iteration**: Using `async/await` and `for await` loops for asynchronous iteration. Keep in mind that each option has its trade-offs, and the best choice depends on your specific use case and requirements.
Related benchmarks:
for loop vs. lodash range foreach vs. jquery each
for vs foreach var array = new Array(100);
JQuery each vs for ... of for array
lodash.each vs Array.forEach vs jQuery.each vs for - function call
for vs foreach vs for..of
Comments
Confirm delete:
Do you really want to delete benchmark?