Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexof vs set123
(version: 0)
Comparing performance of:
1 vs 2
Created:
one year ago
by:
Guest
Jump to the latest result
Tests:
1
var myArray = ['a', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1']; var unique = myArray.filter((value, index, array) => array.indexOf(value) === index);
2
var myArray = ['a', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1', 1, 'a', 2, '1']; 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
1397465.4 Ops/sec
2
778283.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll explain the benchmark in detail. **Benchmark Definition** The provided JSON represents a benchmark definition, which outlines the specifications of the test case. In this case, there are two test cases: `1` and `2`. The benchmark is defined as follows: * Both test cases use an array `myArray` with 40 elements. * Test case `1` uses the `filter()` method to find unique values in the array. * Test case `2` uses the spread operator (`...`) to convert the array to a set and then back to an array. **What is being tested** The benchmark tests two different approaches for finding unique values in an array: 1. Using the `filter()` method with a callback function that checks if the index of each value in the original array matches its position in the filtered array. 2. Using the spread operator (`...`) to convert the array to a set and then back to an array. **Options compared** The two options being compared are: 1. **Using filter()**: This approach iterates over the array once, checking each value against all others. It has a time complexity of O(n^2), where n is the length of the array. 2. **Using spread operator (Set)**: This approach converts the array to a set, which automatically removes duplicates. The time complexity of this approach is O(n). **Pros and cons** **Filter() method:** Pros: * No additional dependencies required * Easy to understand and implement Cons: * Time complexity is O(n^2), making it slower than the spread operator for large arrays. **Spread operator (Set):** Pros: * Fastest approach, with a time complexity of O(n) * Uses modern JavaScript features, which can be beneficial for developers familiar with them. Cons: * Requires support for set data structures and the spread operator in older browsers or environments. * May require additional library dependencies if not supported natively. **Library usage** In test case `1`, no libraries are explicitly mentioned. However, the `filter()` method is a built-in JavaScript function. In test case `2`, the spread operator (`...`) is used to convert the array to a set, which is also a built-in JavaScript feature. No additional libraries are required. **Special JS features or syntax** The benchmark uses two modern JavaScript features: 1. **Destructuring assignment**: The spread operator (`...`) is used to convert the array to a set. 2. **Arrow functions**: The `filter()` method callback function uses an arrow function, which is a shorthand way of defining functions. **Alternatives** Other approaches for finding unique values in an array include: 1. Using `reduce()`: This approach can be used with a custom reducer function to find unique values. 2. Using `forEach()`: This approach can be used with a callback function to iterate over the array and check for duplicates. 3. Using a library like Lodash, which provides a `uniqBy()` function specifically designed for this purpose. Note that these alternatives may have different trade-offs in terms of performance, readability, and complexity compared to the benchmark's approaches.
Related benchmarks:
index vs lastindexof empty
index vs lastindexof empty with startIndex set to 0
Array find with indexOf vs includes
String.indexOf vs String.indexOf with the second parameter
String.indexOf(char) vs String.indexOf(char, position)
Comments
Confirm delete:
Do you really want to delete benchmark?