<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.js'></script>
var arr = ['torus', 'gospel', 'bacon', 'moist', 'lodge', 'flip', 'ev', 'usual', 'glans', 'lanka', 'ames', 'stoic', 'weco', 'buff', 'naacp', 'mercy', 'stork', 'rq', 'mace', '37', 'rung', 'shod', 'snap', 'pr', 'horus', 'lane', 'safe', 'tie', 'yeasty', 'topple', 'grist', 'coed', 'sled', 'bosom', 'packet', 'xn', 'bunch', 'rowdy', 'jo', 'prone', 'styli', 'warmth', 'jimmy', 'launch', 'aspire', 'roil', 'ohm', 'doff', 'slice', 'dream', 'roger', 'theme', 'lz', 'minus', 'lusty', '35th', 'acuity', 'eli', 'spool', 'pivot'];
var temp;
$.each(arr, function(index, item) {
temp = item;
});
var temp;
arr.forEach(function(item) {
temp = item;
});
var temp;
for (var i = 0, len = arr.length; i !== len; ++i) {
temp = arr[i];
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
jQuery.each | |
Array.prototype.forEach | |
traditional for loop |
Test name | Executions per second |
---|---|
jQuery.each | 2186992.5 Ops/sec |
Array.prototype.forEach | 8862406.0 Ops/sec |
traditional for loop | 291744.2 Ops/sec |
Overview of the Benchmark
The provided JSON represents a JavaScript microbenchmarking test case, specifically designed to compare the performance of three different approaches: jQuery's .each()
method, Array.prototype.forEach()
, and a traditional for loop.
What is tested?
In this benchmark, we're comparing the execution speed of three ways to iterate through an array of strings:
.each()
method: This approach uses jQuery's iterative function, which calls a callback function on each element in the array.Array.prototype.forEach()
: This is a built-in JavaScript method that iterates over an array and executes a provided callback function for each element.for
loop to access each element in the array.Options compared
The three options are being tested, which means we're comparing their performance under the same conditions:
Pros and Cons of each approach
Here's a brief summary of the pros and cons of each approach:
.each()
method:Array.prototype.forEach()
:Library usage
The test case uses the jQuery library, which provides a convenient and familiar way to iterate over arrays. However, this also introduces an additional layer of abstraction that may impact performance.
Special JS features or syntax
This benchmark does not explicitly use any special JavaScript features or syntax beyond the three options being compared. However, it's worth noting that modern JavaScript engines often have various optimization techniques and caching mechanisms that can affect the performance of different approaches.
Other alternatives
If you'd like to explore other alternatives or similar benchmarks, here are a few examples:
Array.prototype.map()
: Another built-in JavaScript method for transforming arrays.Set
data structure: A native JavaScript collection for storing unique values.forEach()
with callbacks.Feel free to ask if you have any further questions about this benchmark or would like more information on the alternatives listed above!