Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array.forEach vs Object.keys().forEach
(version: 0)
If I have an array, is it faster to use Array.forEach or Object.keys().forEach?
Comparing performance of:
Array.forEach vs Object.keys().forEach
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = (new Array(10000)).fill(0).map((x, i) => { return i + 1 })
Tests:
Array.forEach
arr.forEach(val => console.log(val))
Object.keys().forEach
Object.keys(arr).forEach(key => console.log(arr[key]))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.forEach
Object.keys().forEach
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:128.0) Gecko/20100101 Firefox/128.0
Browser/OS:
Firefox 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.forEach
11.2 Ops/sec
Object.keys().forEach
12.3 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the test case and explain what's being tested. The test case compares two approaches to iterate over an array: 1. `Array.forEach` 2. `Object.keys().forEach` **What is being tested?** The test is checking which approach is faster for iterating over a large array (in this case, 10,000 elements) and logging each element's value to the console. **Options compared:** * `Array.forEach`: A method that calls a provided function once for each element in an array, without returning any value. * `Object.keys().forEach`: A method that calls a provided function once for each key in an object (which is used to iterate over arrays in this case). **Pros and cons of each approach:** * `Array.forEach`: + Pros: - More intuitive and readable, as it's specifically designed for iterating over arrays. - Less magic strings (e.g., no need to use `Object.keys()`). + Cons: - May not be available in older browsers or environments that don't support modern JavaScript features. * `Object.keys().forEach`: + Pros: - More versatile, as it can be used to iterate over objects and arrays (although the latter is less common). - Available in most environments, including older browsers. + Cons: - Less readable, as it involves a bit of "magic" with `Object.keys()`. **Library and syntax:** Neither approach uses a dedicated library. However, `Array.forEach` has been a part of the JavaScript language since ECMAScript 5 (2011), while `Object.keys().forEach` is more recent, introduced in ECMAScript 2018 (Arrow Functions). **Special JS feature or syntax:** None are explicitly mentioned. **Other alternatives:** If you need to iterate over an array and want to explore alternative approaches, consider: * Using `for...of` loops, which have been available since ECMAScript 6 (2015). * Utilizing `forEach` methods from other libraries, such as Lodash or Ramda. * Implementing your own iteration mechanism using indexing or a custom loop. For this specific test case, the two approaches are simple and straightforward. The choice between them ultimately comes down to personal preference, readability considerations, and compatibility with different environments.
Related benchmarks:
Array fill foreach, vs for i loop
map vs forEach Chris
map vs forEach Chris v2b
Array fill map, vs for i loop
Comments
Confirm delete:
Do you really want to delete benchmark?