Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.find
(version: 0)
Comparing performance of:
includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set(a)
Tests:
includes
return a.find(n => n === 9)
lookup
return b.has(9)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
lookup
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
98211240.0 Ops/sec
lookup
169071232.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided JSON and explain what's being tested, the options compared, their pros and cons, and other considerations. **Benchmark Definition** The benchmark is testing two different approaches to find an element in an array: 1. `array.find`: This method finds the first element in the array that satisfies a given condition. 2. `set.has`: This property checks if a value exists in a Set object. **Script Preparation Code** The script prepares two arrays: `a` and `b`. Array `a` contains numbers from 1 to 10, and array `b` is created by converting the elements of `a` into a new Set object `b`. **Options Compared** We have two test cases: 1. **includes**: This benchmark tests the `array.find` method with a callback function that checks if an element is equal to 9. 2. **lookup**: This benchmark tests the `set.has` property by checking if the value 9 exists in set `b`. **Pros and Cons** Here's a brief summary of each approach: 1. **array.find**: * Pros: Efficient for finding a single element, as it uses a single loop iteration. * Cons: May not be suitable for large arrays or when searching for multiple elements, as it returns the first match. 2. **set.has**: * Pros: Fast and efficient for checking membership in a set, as sets use a hash table internally to store elements. * Cons: Slower than `array.find` because it involves traversing the Set object's internal data structure. **Other Considerations** When comparing these two approaches, we should also consider factors like: * Array size and density (i.e., how spread out the elements are) * The presence of duplicate elements in the array * The need for finding multiple elements or performing other operations on the set **Library Usage** In this benchmark, the `Set` object is used from the JavaScript Standard Library. A Set is a collection of unique values, and its `has` property provides an efficient way to check if a value exists in the set. **Special JS Feature/Syntax** There are no special JavaScript features or syntaxes being tested in this benchmark. The tests only involve standard JavaScript methods and properties. **Alternatives** If you were to recreate this benchmark using alternative approaches, you might consider: * Using other data structures like `Map` or a custom implementation of a set * Employing different search algorithms, such as binary search for sorted arrays or more complex algorithms for unsorted arrays * Comparing the performance of these methods with those from other programming languages (e.g., using the same method in C++ or Python) Keep in mind that this benchmark focuses specifically on the `array.find` and `set.has` methods. If you wanted to compare the performance of different array-related methods, such as `indexOf`, `includes`, or `every`, you would need to create a separate set of benchmarks.
Related benchmarks:
convert to set + set.has vs. array.includes
Includes (array) vs Has (Set)
Small n set vs array
set vs array find if exists
set vs array find if exists v2
Comments
Confirm delete:
Do you really want to delete benchmark?