Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes large 3
(version: 0)
Comparing performance of:
includes vs lookup
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = []; for(let i = 0; i < 2000; i++){ a.push(i); } var b = new Set(a)
Tests:
includes
return a.includes(1000)
lookup
return b.has(1000)
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:
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 is comparing two approaches to check if an element exists in a set or array: `set.has` (lookup) vs. `array.includes`. **Script Preparation Code** ```javascript var a = []; for (let i = 0; i < 2000; i++) { a.push(i); } var b = new Set(a); ``` This code creates an array `a` with 2000 elements and a set `b` that contains the same elements as `a`. The script preparation is done before running the actual benchmark, which means it's executed once for both test cases. **Test Cases** There are two test cases: 1. **includes**: This test case runs the expression `a.includes(1000)`. 2. **lookup**: This test case runs the expression `b.has(1000)`. Both expressions will check if the element 1000 exists in either the array or set. **Options Compared** The two options being compared are: * `array.includes()`: A method that checks if an element exists in an array. * `set.has()`: A method that checks if an element exists in a set. **Pros and Cons of Each Approach** * **Array Includes**: Pros: + Wide support across browsers and platforms. + Can be used with arrays of any type, not just integers. Cons: + May have performance issues for large arrays due to the need to iterate over all elements. + May be slower than set-based lookups for small sets. * **Set Has**: Pros: + Fast lookup times, especially for large sets (average O(1) time complexity). + More memory-efficient than using an array. Cons: + Limited support across browsers and platforms (not all browsers support sets). + May not work with arrays of arbitrary types. **Library/Function Used** * `Set`: A built-in JavaScript object that represents a collection of unique values. It's used in the script preparation code to create a set `b` containing the elements of array `a`. **Special JS Feature/Syntax** None mentioned in this benchmark. **Other Considerations** When choosing between `array.includes()` and `set.has()`, consider the following: * If you need to check for membership in an array or set frequently, using `set.has()` might be a better choice due to its faster lookup times. * However, if you're working with arrays of arbitrary types or need wide browser support, `array.includes()` might be a more suitable option. **Alternatives** Other alternatives to `array.includes()` and `set.has()` include: * Using `Object.prototype.hasOwnProperty.call(array, value)` for array lookups (less efficient than `includes()`). * Using `Array.from(set).indexOf(value) + 1` for set lookups (less efficient than `has()`). * Implementing a custom lookup function using a hash table or similar data structure. Keep in mind that these alternatives may have different performance characteristics and trade-offs depending on the specific use case.
Related benchmarks:
set.has vs. array.includes (1 million entries)
set.has vs. array.includes - large array
set vs array iteration 100k elements
3set vs array iteration New doge333
Comments
Confirm delete:
Do you really want to delete benchmark?