Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Native forEach vs jQuery.each
(version: 0)
Comparing performance of:
Native foreach vs jQuery each
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
Script Preparation code:
var arr = Array(1000).fill().map(() => Math.floor(100 * Math.random()));
Tests:
Native foreach
arr.forEach(item => { });
jQuery each
$.each(arr, item => { });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native foreach
jQuery each
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Native foreach
2684321.5 Ops/sec
jQuery each
756377.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested, compared, and the pros/cons of different approaches. **Benchmark Definition** The benchmark is comparing two JavaScript methods: `forEach` (native) and `$.each` (jQuery). The goal is to measure which one performs better in terms of execution speed on a given dataset. **Script Preparation Code** The script preparation code creates an array of 1000 random numbers using the `Array` constructor, `fill()`, and `map()` methods: ```javascript var arr = Array(1000).fill().map(() => Math.floor(100 * Math.random())); ``` This code is executed before running the benchmark. **Html Preparation Code** The HTML preparation code includes a script tag that loads the jQuery library from a CDN: ```html <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script> ``` This code is executed before running the benchmark as well. **Individual Test Cases** There are two test cases: 1. **Native `forEach`**: This test case runs the native JavaScript `forEach` method on the prepared array: ```javascript arr.forEach(item => { }); ``` 2. **jQuery `.each()`**: This test case runs the jQuery `.each()` method on the prepared array, which is essentially equivalent to using a traditional `for` loop: ```javascript $.each(arr, item => { }); ``` **Benchmark Results** The benchmark results show two browsers (Firefox 121) running the same test cases. The "Native `forEach`" test case has an execution speed of approximately 456,411 executions per second, while the ".jQuery `.each()`" test case has a slower execution speed of around 340,620 executions per second. **Pros and Cons** Here are some pros and cons of each approach: * **Native `forEach`**: + Pros: Native JavaScript method, no additional library overhead. + Cons: May not be optimized for performance, can be slower due to engine limitations. * **jQuery `.each()`**: + Pros: Optimized for performance by jQuery's engine, provides a more traditional iteration experience. + Cons: Requires an additional library (jQuery) to be loaded. **Library - jQuery** The `$.each` method is part of the jQuery library, which provides a set of DOM manipulation and event handling APIs. In this benchmark, jQuery is used as a library to provide an alternative iteration mechanism. Note that in modern JavaScript environments, you can use other libraries like Lodash or Ramda for iteration purposes, but they are not mentioned here. **Special JS Feature - None** There are no special JS features or syntax used in these test cases. The focus is on the fundamental performance comparison between native `forEach` and jQuery `.each()`. **Other Alternatives** Other alternatives to iterate over arrays in JavaScript include: * Traditional `for` loops * Array methods like `map()`, `filter()`, and `reduce()` * Higher-order functions from libraries like Lodash or Ramda These alternatives may offer different trade-offs between performance, readability, and maintainability.
Related benchmarks:
Array loop vs foreach vs map proper
Reverse array loop vs foreach vs map
JS Array loop vs foreach vs map vs for of
Array loop vs foreach vs map with Random
Array loop vs foreach vs every
Comments
Confirm delete:
Do you really want to delete benchmark?