Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Extract prop from object array
(version: 0)
Comparing performance of:
Using Array.prototype.map() vs Using traditional for loop vs Using ES6 for..of
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testArray = [] for(var i=0;i<100000;i++){ testArray.push({ id: i, val: Math.random()}); }
Tests:
Using Array.prototype.map()
var vals = testArray.map((a) => {return a.val});
Using traditional for loop
var vals=[]; for(var i=0;i<testArray.length;i++){ vals.push(testArray[i].val); }
Using ES6 for..of
var vals=[]; for(var item of testArray){ vals.push(item.val); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Using Array.prototype.map()
Using traditional for loop
Using ES6 for..of
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark tests the efficiency of three different ways to extract the `val` property from an array of objects in JavaScript: **1. Using `Array.prototype.map()`:** - This method leverages the built-in `.map()` function which iterates over each element in the array and applies a provided function to it, returning a new array with the results. - **Pros:** Concise, readable, and generally considered the most efficient approach for tasks like this. It's optimized for JavaScript engines. - **Cons:** None significant in this context. **2. Using a traditional `for` loop:** - This method uses a classic `for` loop to iterate over the indices of the array and access each element individually. - **Pros:** More familiar to developers who learned programming before JavaScript's higher-order functions. - **Cons:** Less concise than `.map()`, can be harder to read, and generally slower due to manual iteration. **3. Using ES6 `for...of` loop:** - This method utilizes the more modern `for...of` loop, which iterates directly over the values of the array rather than its indices. - **Pros:** More concise and readable than the traditional `for` loop, can be slightly faster in some cases. - **Cons:** Requires understanding of ES6 syntax, might not always be significantly faster than `.map()`. **Other Alternatives:** - **`reduce()`:** While not as directly applicable here, JavaScript's `reduce()` function could be used to accumulate all the `val` properties into a single array. This would be less efficient for extracting individual values but useful for other tasks. - **Libraries:** There aren't any specific libraries used in this benchmark. However, in real-world applications, you might use libraries like Lodash or Ramda which offer optimized versions of common array operations. Let me know if you have any more questions!
Related benchmarks:
Get last array element
Extract prop from object array, shorter arrow function syntax
Fill array with random integers
Array push vs
Comments
Confirm delete:
Do you really want to delete benchmark?