Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter map vs reduce vs for of vs for in vs for length
(version: 5)
Comparing performance of:
filter map vs reduce vs for of vs for in vs for length
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var test = [] for(let i=1; i<200000; i++) { test.push({ id: i, class: Math.floor(Math.random()*10) }) }
Tests:
filter map
test.filter(item => item.class ==2).map(item => item.id)
reduce
test.reduce((acc, item) => { if(item.class==2) { acc.push(item.id) return acc } return acc }, [])
for of
let acc = [] for(const item of test) { if(item.class==2) { acc.push(item.id) } }
for in
let acc = [] for(const indx in test) { const item = test[indx] if(item.class==2) { acc.push(item.id) } }
for length
let acc = [] for(let i=1; i<test.length; i++) { const item = test[i] if(item.class==2) { acc.push(item.id) } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
filter map
reduce
for of
for in
for length
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
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Purpose** The benchmark is designed to compare the performance of four different JavaScript iteration methods: `filter`, `map`, `reduce`, and two custom approaches using traditional `for` loops (`for in` and `for length`). The goal is to determine which method is most efficient for filtering and extracting data from a large array. **Options being compared** The four options being tested are: 1. **`filter`**: This method creates a new array with only the elements that pass the test implemented by the provided function. 2. **`map`**: This method creates a new array populated with the results of calling a provided function on every element in the calling array. 3. **Custom `for in` loop**: This approach uses an index variable to iterate over the array, checking each element's class and pushing ID to an accumulator array if it matches. 4. **Custom `for length` loop**: Similar to the custom `for in` loop, but uses the `length` property of the array to iterate over its elements. **Pros and Cons** Here's a brief overview of the pros and cons for each option: * **`filter`**: Pros: concise, readable. Cons: creates a new array, potentially inefficient if array is large. * **`map`**: Pros: concise, readable. Cons: also creates a new array, may not be efficient for very large arrays. * **Custom `for in` loop**: + Pros: flexible, can handle any iteration logic. + Cons: verbose, error-prone due to index management. * **Custom `for length` loop**: Similar pros and cons as the custom `for in` loop. **Library usage** There is no explicit library mentioned in the benchmark. However, it's likely that the `filter`, `map`, and other built-in array methods are implemented using some underlying libraries or frameworks. **Special JavaScript features/syntax** This benchmark does not use any special JavaScript features or syntax beyond the basic iteration methods being tested. **Other alternatives** If you're interested in exploring alternative iteration methods, here are a few examples: * **`forEach`**: Similar to `map`, but doesn't return a new array. Instead, it executes a callback function on each element of an array. * **`while` loops**: Another traditional looping approach that can be used for iterating over arrays. * **`Array.prototype.forEach() with a custom iterator`**: This approach uses a custom iterator to iterate over the array, allowing for more control over iteration logic. Keep in mind that these alternatives may have different performance characteristics and use cases compared to the options being tested in this benchmark.
Related benchmarks:
slice vs filter more than 1000
slice vs filter 2
slice vs filter (10000000)
Shorten array -- slice vs filter
Comments
Confirm delete:
Do you really want to delete benchmark?