Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs manual search
(version: 0)
Comparing performance of:
Set vs Manual
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr = Array.from({ length: 1000 }, (_, i) => i)
Tests:
Set
const set = new Set(arr) arr.map((item) => set.has(item))
Manual
arr.map((item1) => { arr.some((item2) => item1 === item2) })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Manual
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
28283.4 Ops/sec
Manual
3092.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark you provided. **Benchmark Overview** The benchmark compares two approaches for searching an array: using a `Set` data structure and manual iteration (using `some()`). The test creates a large array of 1000 elements, prepares the script and HTML, and then runs each test case multiple times. The results are recorded and compared to determine which approach is faster. **Options Compared** The two options being tested are: 1. **Set**: Using a `Set` data structure to search for an element in the array. A `Set` is a collection of unique values that allows for efficient lookup, insertion, and deletion. 2. **Manual**: Manual iteration using the `some()` method, which checks if at least one element in the array matches a certain condition. **Pros and Cons** 1. **Set**: * Pros: + Faster performance due to optimized lookup operations + More memory-efficient than manual iteration * Cons: + Requires additional memory for storing the Set data structure + May have higher overhead for creation and maintenance of the Set 2. **Manual**: + Pros: + Lowers memory usage by not requiring an extra data structure + Simplifies code for some developers * Cons: + Slower performance due to linear iteration over the array + More prone to errors and bugs, especially with large datasets **Library Used** None explicitly mentioned in this benchmark. However, it's likely that a JavaScript engine or runtime environment (e.g., V8) is used to execute the scripts. **Special JS Feature/Syntax** No special features or syntax are used in this benchmark. Both `Set` and manual iteration rely on standard JavaScript constructs. **Other Alternatives** If you're considering alternative approaches for searching an array, here are some options: 1. **Array.prototype.includes()**: This method is a more modern and efficient way to search for an element in an array, using binary search under the hood. 2. **Array.prototype.findIndex()**: This method returns the index of the first occurrence of an element that satisfies the provided callback function, or -1 if no such element exists. 3. **Native JavaScript arrays with built-in methods**: Depending on the browser or environment, some browsers provide built-in methods like `indexOf()` or `includes()` for searching arrays. In summary, this benchmark provides a simple and straightforward comparison of two approaches: using a `Set` data structure and manual iteration. The results can be useful in understanding the performance differences between these approaches, which may have implications for real-world applications that require efficient array search operations.
Related benchmarks:
`Array.slice(-1)[0]` vs `Array[Array.length]` for 10000 length
Array: get last item
For with variable
Shift vs Slice
`array.slice(-1)[0]` vs `array[array.length - 1]`
Comments
Confirm delete:
Do you really want to delete benchmark?