Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
index vs map115
(version: 0)
Comparing performance of:
1 vs 2
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var myArray = []; for (let i = 0; i < 9999; i++) { myArray.push(i) }
Tests:
1
var unique = myArray.filter((value, index, array) => array.indexOf(value) === index);
2
let unique = [...new Set(myArray)];
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
1
2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36
Browser/OS:
Chrome 129 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
1
106.7 Ops/sec
2
2473.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark. **Benchmark Overview** The provided benchmark measures the performance of two approaches for finding unique elements in an array: 1. `Array.prototype.filter()` 2. `Set` (using spread operator) **Approach 1: `Array.prototype.filter()`** This approach uses the `filter()` method to create a new array with only the unique elements. The callback function passed to `filter()` checks if the current value is equal to its index in the original array. **Pros and Cons:** Pros: * Simple to implement * Works for all browsers Cons: * Has a time complexity of O(n^2) in the worst case, making it slower than other approaches for large arrays. * Creates a new array with duplicate elements if there are duplicates in the original array. **Approach 2: `Set` using spread operator** This approach uses the `Set` object and the spread operator (`...`) to find unique elements. The callback function passed to `filter()` creates a set of values from the array, which automatically removes duplicates. Pros: * Has a time complexity of O(n) in the worst case, making it faster than other approaches for large arrays. * Returns an array with only unique elements. Cons: * Requires modern browsers that support the `Set` object and spread operator. * May not be compatible with older browsers or environments that don't support these features. **Library and Special JS Features** In this benchmark, no libraries are explicitly mentioned. However, the use of the spread operator (`...`) is a modern JavaScript feature introduced in ECMAScript 2015 (ES6). **Other Alternatives** Before using `Array.prototype.filter()` or the `Set` approach, other alternatives can be considered: * Using `Map` to find unique elements * Using `reduce()` method to accumulate unique values * Using a custom implementation with loops and conditional statements However, these approaches are generally less efficient and more complex than the two options presented in this benchmark. **Benchmark Preparation Code** The script preparation code creates an array of 9999 elements, which is used as input for both benchmark cases. The `unique` variable is created using each approach (filter() and Set), respectively. **Individual Test Cases** There are two test cases: 1. Using `Array.prototype.filter()` with a callback function 2. Using the spread operator (`Set`) to find unique elements Each test case has a corresponding benchmark result, which shows the performance metrics for each approach.
Related benchmarks:
for vs map
for vs foreach vs map 2
JavaScript Array.prototype.map vs loop+Array.prototype.push ( big array )
fill + map vs push
flatmap vs for of
Comments
Confirm delete:
Do you really want to delete benchmark?