Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array Diffs
(version: 0)
Filter vs For loop vs Set vs Reduce
Comparing performance of:
Filter vs For loop vs Set vs Reduce
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Filter
const array1 = [1, 2, 3, 4, 5]; const array2 = [3, 4, 5, 6, 7]; const difference = array1.filter((element) => !array2.includes(element));
For loop
const array1 = [1, 2, 3, 4, 5]; const array2 = [3, 4, 5, 6, 7]; const difference = []; for (let i = 0; i < array1.length; i++) { if (array2.indexOf(array1[i]) === -1) { difference.push(array1[i]); } }
Set
const array1 = [1, 2, 3, 4, 5]; const array2 = [3, 4, 5, 6, 7]; const set1 = new Set(array1); const set2 = new Set(array2); const difference = [...set1].filter( (element) => !set2.has(element));
Reduce
const array1 = [1, 2, 3, 4, 5]; const array2 = [3, 4, 5, 6, 7]; const difference = array1.reduce((result, element) => { if (array2.indexOf(element) === -1) { result.push(element); } return result; }, []);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Filter
For loop
Set
Reduce
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15
Browser/OS:
Safari 17 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Filter
5361588.5 Ops/sec
For loop
35094612.0 Ops/sec
Set
2284013.8 Ops/sec
Reduce
14183769.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. **Benchmark Overview** The benchmark measures the performance of different approaches to find the differences between two arrays: `filter`, `for loop`, `set`, and `reduce`. **Options Compared** Here are the options being compared, along with their pros and cons: 1. **Filter** * Code: `const difference = array1.filter((element) => !array2.includes(element));` * Pros: + Simple and easy to understand. + Efficient for small arrays. * Cons: + May be slower for large arrays due to the overhead of filtering. 2. **For loop** * Code: `for (let i = 0; i < array1.length; i++) { if (array2.indexOf(array1[i]) === -1) { difference.push(array1[i]); } }` * Pros: + Can be efficient for large arrays. * Cons: + More complex and harder to understand than filter. 3. **Set** * Code: `const set1 = new Set(array1); const set2 = new Set(array2); const difference = [...set1].filter((element) => !set2.has(element));` * Pros: + Efficient for large arrays, as sets provide O(1) lookup times. * Cons: + Requires creating a new set, which can be memory-intensive. 4. **Reduce** * Code: `const difference = array1.reduce((result, element) => { if (array2.indexOf(element) === -1) { result.push(element); } return result; }, []);` * Pros: + Can be efficient for large arrays, as it uses a single loop. * Cons: + More complex and harder to understand than filter. **Library: Set** The `Set` library is used in the "Set" option. A set in JavaScript is an unordered collection of unique values. It provides O(1) lookup times for members, making it efficient for large arrays. **Special JS Feature/Syntax: None** There are no special JavaScript features or syntaxes being tested in this benchmark. **Other Considerations** * Memory usage: The "Set" option may consume more memory due to the creation of new sets. * Cache locality: The "For loop" option may exhibit better cache locality, as it accesses elements sequentially. * Interpreter overhead: The "Filter", "Set", and "Reduce" options may be affected by interpreter overhead, such as garbage collection. **Alternatives** If you're looking for alternatives to this benchmark, you could consider: * Using a different data structure, such as an array or a linked list, instead of sets. * Using a different algorithm, such as a parallel or concurrent approach. * Adding more complexity to the input data or test cases. * Using a different programming language or framework. Keep in mind that these alternatives would require significant changes to the benchmark and may not provide comparable results.
Related benchmarks:
Lodash difference vs JS filter and includes
Filter vs Set (unique elements)
Diff size Set vs Filter for unique
Set from array vs array Filter unique
Comments
Confirm delete:
Do you really want to delete benchmark?