Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
reduce + map filter
(version: 0)
Comparing performance of:
reduce vs map
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var customers = [ { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: true, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" }, { active: false, name: "a", email: "b" } ]; Array.prototype.filterMap = function(filter) { const r = []; const len = this.length; for(let i=0; i<len; i++) { const item = filter(this[i], i, this); if (item !== undefined) { r.push(item); } } return r; };
Tests:
reduce
var emails = customers.filter( (c) => c.active) .map( (c) => ({ name: c.name, email: c.email }));
map
var emails = customers.filterMap( (c) => c.active ? { name: c.name, email: c.email } : undefined);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reduce
map
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 performance of two different approaches to filtering and mapping an array of customer objects in JavaScript: **Option 1: `reduce`** * **Benchmark Definition:** ```javascript var emails = customers.filter( (c) => c.active)\r\n .map( (c) => ({ name: c.name, email: c.email })); ``` This approach uses the `.filter()` method to select active customers and then the `.map()` method to transform each active customer object into a new object containing just the `name` and `email`. **Option 2: `filterMap` (Custom Method)** * **Benchmark Definition:** ```javascript var emails = customers.filterMap(\r\n (c) => c.active ?\r\n { name: c.name, email: c.email } :\r\n undefined); ``` This approach utilizes a custom method called `filterMap` which combines the functionality of `.filter()` and `.map()` into a single step. It iterates through the array, applies the provided function to each element, and only pushes non-undefined results into a new array. **Pros and Cons:** * **`reduce` approach:** * **Pros:** More widely known and understood, utilizes standard JavaScript methods. * **Cons:** Requires two separate method calls which can lead to slightly more overhead. * **`filterMap` approach:** * **Pros:** Potentially more concise and efficient by combining filtering and mapping into one step. * **Cons:** Less common, relies on a custom implementation that might not be as optimized as built-in methods. **Other Considerations:** * **Array Size:** The performance difference between the two approaches may become more noticeable with larger arrays. * **Complexity of Transformation:** If the transformation logic within `.map()` is complex, it could outweigh the potential efficiency gains of `filterMap`. **Alternatives:** * **Functional Programming Libraries:** Libraries like Lodash or Ramda offer optimized versions of functions like `filter` and `map`, potentially improving performance further. * **Destructuring Assignment:** If you're only interested in specific properties from the customer objects, destructuring assignment within `.filter` and `.map` can make the code more concise and readable. Let me know if you have any more questions!
Related benchmarks:
kjnzjv
splice + spread vs filter to remove one item at given index
Map + filter vs reduce
map and filter vs reduce v2
Comments
Confirm delete:
Do you really want to delete benchmark?