Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
filter vs reduce relationships
(version: 0)
Comparing performance of:
filter & includes vs reduce & indexOf
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var relationships = [{source_ref: "blahblahsighting"},{source_ref: "blahblahsighting"},{source_ref: "blahblahsighting"},{source_ref: "blahblahsing"},{source_ref: "blahblahsing"},{source_ref: "blahblahsighting"}]
Tests:
filter & includes
relationships.filter((relationship) => relationship.source_ref.includes("sighting"))
reduce & indexOf
relationships.reduce((mem, relationship) => { if (relationship.source_ref.indexOf("sighting")) { mem.push(relationship) } return mem; }, [])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
filter & includes
reduce & indexOf
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 dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark is designed to compare the performance of two approaches: using the `Array.prototype.filter()` method versus using `Array.prototype.reduce()` method, both with a custom function that checks if the `source_ref` property includes or starts with the string "sighting". **Options Compared** Two options are being compared: 1. **filter() + includes**: This option uses the `Array.prototype.filter()` method to create a new array containing only the elements for which the provided function returns true. The custom function checks if the `source_ref` property includes the string "sighting" using the `includes()` method. 2. **reduce() + indexOf**: This option uses the `Array.prototype.reduce()` method to accumulate values in an array, starting with an empty array. For each element, it checks if the `source_ref` property starts with the string "sighting" using the `indexOf()` method. **Pros and Cons of Each Approach** Here are some pros and cons of each approach: **filter() + includes:** Pros: * **Easier to read and understand**: The code is more straightforward, as it's using a well-known method (`filter()`) with a simple condition. * **Less memory allocation**: Creating a new array with `filter()` doesn't require allocating memory for the entire array. Cons: * **Potential performance overhead**: The `includes()` method can be slower than other comparison methods (like `indexOf()` or `startsWith()`), especially for large strings. **reduce() + indexOf:** Pros: * **More efficient string matching**: The `indexOf()` method is generally faster and more accurate than the `includes()` method. * **Less allocation memory**: Like `filter()`, creating an empty array with `reduce()` doesn't require allocating memory for the entire array. Cons: * **Complexer to read and understand**: While `reduce()` is a well-known method, it's often less intuitive to use compared to `filter()`. * **More memory overhead**: The accumulator (`mem`) needs to be initialized and pushed onto the array, which can lead to more memory allocation. **Library Used** In this benchmark, no external library is used. However, some JavaScript engines might implement their own optimization techniques that aren't part of the standard API. **Special JS Feature or Syntax** None are explicitly mentioned in the provided code. However, keep in mind that different JavaScript engines and implementations may have subtle differences or optimizations not covered by the standard API. **Other Alternatives** To compare these approaches, you could also try: * Using `Array.prototype.some()` instead of `filter()` * Using a custom loop with `forEach()` or `for...of` instead of `reduce()` For example: ```javascript // filter() const result = relationships.filter(relationship => relationship.source_ref.includes("sighting")); // reduce() const result2 = relationships.reduce((mem, relationship) => { if (relationship.source_ref.indexOf("sighting") >= 0) mem.push(relationship); return mem; }, []); ``` Keep in mind that the performance differences might be smaller compared to using `filter()` and `includes()`.
Related benchmarks:
filter.map vs reduce 2
reduce vs filter
Flatmap vs reduce with objects
Reduce vs map with empty filter
Filter vs toSpliced
Comments
Confirm delete:
Do you really want to delete benchmark?