Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
array.entries() vs for loop 2
(version: 1)
Comparing performance of:
for loop vs array.entries()
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var data = Array.from(Array(100000).keys())
Tests:
for loop
const acc = {} for (let i = 0; i < data.length; i++) { acc[i] = data[i].toString() }
array.entries()
const acc2 = {} for(const [index, value] of data.entries()) { acc2[index] = value.toString() }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
for loop
array.entries()
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
for loop
215.0 Ops/sec
array.entries()
169.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. **Benchmark Definition** The benchmark is comparing two approaches to iterating over an array: using a traditional `for` loop and using the `entries()` method of arrays, which was introduced in ECMAScript 2015 (ES6). **Options Compared** 1. **Traditional `for` loop**: This approach uses a manual index variable (`i`) to iterate over the array elements. 2. **Array `entries()` method**: This approach uses the `entries()` method to get an iterator object, which yields arrays of tuples containing the index and value of each element in the original array. **Pros and Cons** 1. **Traditional `for` loop**: * Pros: More widely supported, no dependency on modern browsers. * Cons: Error-prone, requires manual indexing, less readable code. 2. **Array `entries()` method**: * Pros: Modern, concise, and expressive syntax, leverages built-in iterator functionality. * Cons: Less widely supported (older browsers), may incur performance overhead due to the creation of an iterator object. In general, if you need to support older browsers or require more control over iteration, a traditional `for` loop might be a better choice. However, for modern web development and projects targeting recent browser versions, the `entries()` method is often a more elegant and efficient solution. **Library/Technology Used** None explicitly mentioned in this benchmark definition. The `Array.entries()` method is a built-in JavaScript feature introduced in ES6. **Special JS Feature/Syntax** * **`for...of` loop**: Not used directly here, but related to the `entries()` method. + Introduced in ECMAScript 2015 (ES6), this syntax allows iterating over arrays using an iterator object. + The `for...of` loop is a more concise and expressive way to iterate over arrays, as seen in the benchmark's "array.entries()" test case. **Alternatives** Other alternatives for iterating over arrays include: 1. **Array.prototype.forEach()**: A synchronous method that executes a callback function once for each element in an array. 2. **Array.prototype.map()`, **Array.prototype.filter()`, and **Array.prototype.reduce()**: These methods provide more functional programming-style iteration, but might incur additional overhead or change the original data structure. For this specific benchmark, the options being compared are designed to highlight the performance differences between traditional `for` loop iteration and the modern `entries()` method approach.
Related benchmarks:
Array.forEach vs Object.keys().forEach
set vs array iteration many
array[0] vs array.at(0) on 100000 elements
Object.fromEntries vs for at scale
Comments
Confirm delete:
Do you really want to delete benchmark?