Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Unique via Set vs Filter
(version: 0)
Comparing performance of:
Filter vs Set
Created:
9 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var filterMyUniques = ['lorem', 'ipsum', 'dolor', 'sit', 'amet', 'consectetur', 'adipiscing', 'elit', 'Maecenas', 'consectetur', 'vehicula', 'interdum', 'etiam', 'id', 'dui', 'pellentesque', 'felis', 'ultricies', 'fringilla', 'Phasellus', 'mollis', 'ligula', 'quis', 'nibh', 'fringilla', 'consectetur', 'in', 'nec', 'augue', 'vel', 'risus', 'condimentum', 'hendrerit', 'Aenean', 'sapien', 'est', 'malesuada', 'ut', 'ipsum', 'ac', 'feugiat', 'mattis', 'tellus', 'nam', 'consequat', 'rutrum', 'augue', 'nec', 'volutpat', 'sapien', 'euismod', 'eu', 'Integer', 'vel', 'faucibus', 'leo', 'et', 'ultricies', 'turpis', 'phasellus', 'eleifend', 'erat', 'sit', 'amet', 'eleifend', 'maximus', 'augue', 'nisi', 'ullamcorper', 'ligula', 'sed', 'pharetra', 'velit', 'arcu', 'et', 'nibh', 'Proin', 'ut', 'fringilla', 'eros'];
Tests:
Filter
var uniques = filterMyUniques.filter( function( item, index, list ){ return list.indexOf( name ) === index; } )
Set
var set = {}; filterMyUniques.forEach( function( item, index, list ){ set[item] = 1; } ) var uniques = Object.keys( set );
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Filter
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 JSON and explain what's being tested. **Benchmark Definition** The benchmark definition is a JavaScript script that creates an array of unique strings using two different methods: `filter()` and `Set`. The `filterMyUniques` array contains 40 unique strings. The goal of this benchmark is to compare the performance of these two approaches in creating an array of unique elements. **Options Compared** The two options being compared are: 1. **Filter() method**: This method uses a callback function to iterate over the array and filter out duplicates based on the `indexOf()` method. 2. **Set object**: This approach uses a Set object to store unique values from the `filterMyUniques` array. **Pros and Cons** * **Filter() method**: + Pros: Easy to understand, straightforward implementation, works well for small datasets. + Cons: Can be slow for large datasets due to the use of `indexOf()` and loop iteration. Also, it can lead to performance issues if the dataset is very large or if there are many duplicate elements in the array. * **Set object**: + Pros: Fast and efficient for creating a set of unique values. It uses a hash table data structure under the hood, which provides constant-time complexity for adding and looking up elements. + Cons: Requires modern JavaScript features (ECMAScript 2015+) and can be less intuitive to implement than the `filter()` method. **Library and Purpose** In this case, there is no explicit library being used. However, it's worth noting that the `Set` object is a built-in feature in modern JavaScript engines. **Special JS Features or Syntax** There are no special JavaScript features or syntax being tested in this benchmark. Both approaches use standard JavaScript methods and data structures. **Other Alternatives** If you're looking for alternative ways to create a set of unique values, here are a few options: 1. **Using `Array.prototype.reduce()`**: You can use the `reduce()` method to create a Set-like object. 2. **Using `Array.prototype.forEach()` with a callback function**: Similar to the `Set` approach, you can use `forEach()` with a callback function to iterate over the array and add unique values to an object. 3. **Using `Map` object**: You can use a `Map` object to store unique values, where each key is a unique value from the array. Here's some sample code for these alternatives: ```javascript // Using Array.prototype.reduce() const uniques = []; filterMyUniques.forEach(function(item) { const existingIndex = uniques.findIndex((existingItem) => existingItem === item); if (existingIndex === -1) { uniques.push(item); } }); // Using Array.prototype.forEach() with a callback function const uniques = {}; filterMyUniques.forEach(function(item) { uniques[item] = true; }); ``` ```javascript // Using Map object const uniques = new Map(); filterMyUniques.forEach(function(item) { if (!uniques.has(item)) { uniques.set(item, true); } }); ``` These alternatives may have different performance characteristics and trade-offs compared to the `Set` approach.
Related benchmarks:
jQuery.each() vs Array.prototype.forEach()
every vs set2
Some vs. Filter vs. indexOf vs. includes vs. Find vs. FindIndex
jQuery $.each() vs Array.prototype.forEach()
Comments
Confirm delete:
Do you really want to delete benchmark?