Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
test filtering with set
(version: 0)
Comparing performance of:
includes vs set
Created:
6 years ago
by:
Guest
Jump to the latest result
Tests:
includes
const a = [1,2,3,4,5,2,4,6,1,6,4,2,5]; const b = []; a.forEach(n => !b.includes(n) && b.push(n));
set
const a = [1,2,3,4,5,2,4,6,1,6,4,2,5]; const b = [...new Set(a)];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
set
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, compared, and the pros and cons of each approach. **Benchmark Overview** The benchmark is designed to compare two approaches: using `includes()` method with array iteration, and using a Set data structure. The test cases are: 1. **`includes`**: Iterates over an array `a`, pushes each element into another array `b` if it's not already present using the `includes()` method. 2. **`set`**: Creates a new Set from an array `a` and then converts it back to an array, which is assigned to variable `b`. **Library and Syntax** In both test cases, there are no specific libraries or advanced JavaScript features used beyond what's part of the standard library. **Approach Comparison** Both approaches aim to achieve the same result: filtering out duplicates from an array. However, they differ in their implementation: 1. **`includes()` method with array iteration**: This approach is straightforward but inefficient due to its O(n^2) complexity. For each element in `a`, it iterates over `b` to check if the element exists using `includes()`. If not found, it's pushed into `b`. * Pros: Simple and easy to understand. * Cons: Slow for large arrays (~ O(n^2)). 2. **Using a Set**: This approach uses a data structure optimized for fast lookups, reducing the time complexity to O(n). It creates a new Set from `a`, which automatically removes duplicates, and then converts it back to an array using spread operator (`[...new Set(a)]`). * Pros: Fast (~ O(n)) and efficient. * Cons: Requires creating a temporary Set, which may have some overhead. **Benchmark Result Analysis** The latest benchmark result shows that: 1. **`includes()`**: Takes approximately 4.82 million executions per second on Chrome 75 running on Mac OS X 10.13.6. 2. **`set`**: Takes around 1.71 million executions per second on the same platform. Based on these results, using a Set appears to be significantly faster for this specific test case. **Other Alternatives** For large datasets or performance-critical applications, other approaches might include: * Using `Array.from()` and `Set.prototype.forEach()`, which is similar to the `set` approach but uses more modern methods. * Implementing custom hash functions for the array elements, allowing for faster lookups using a Map or similar data structure. Keep in mind that these alternative approaches would require additional setup and might not be as straightforward to understand.
Related benchmarks:
Filter unique, Set vs filter()
filter vs some
Filter vs Set (unique elements)
Filter vs set
Set from array vs array Filter unique
Comments
Confirm delete:
Do you really want to delete benchmark?