Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
intersect by create && set.has vs. array.includes
(version: 0)
Comparing performance of:
includes vs lookup
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = ['daf1313', '134132', 'dfasdf123', 'asfdgasfg4', 'sgfdafgs5', '134136', 'sgfdhjghj7', 'dfhjhfdj8', 'hjhfdjfha9', 'afgfagfga10'];
Tests:
includes
return a.includes('notinarray')
lookup
var b = new Set(a) return b.has('notinarray')
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:
6 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.6 Safari/605.1.15
Browser/OS:
Safari 18 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
55476852.0 Ops/sec
lookup
8425455.0 Ops/sec
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 Overview** The benchmark is designed to compare two approaches for checking if an element exists in an array: 1. Using the `includes` method with a string literal ("notinarray"). 2. Creating a new Set object from the array and using the `has` method on that set. **Options Compared** We have two options being compared: **Option 1: `includes` method** * Description: The `includes` method is a part of the JavaScript Array prototype, which checks if an element exists in the array. It returns a boolean value indicating whether the element is present. * Pros: + Simple and intuitive to use. + Fast execution speed (typically O(n), where n is the length of the array). * Cons: + Can be slower for very large arrays, as it needs to scan through the entire array to check if the element exists. **Option 2: Creating a Set from the array and using `has`** * Description: This approach involves creating a new Set object from the array. A Set is an unordered collection of unique values. The `has` method on a Set object checks if a specific value (in this case, "notinarray") exists within the set. * Pros: + Can be faster for very large arrays, as it uses a data structure optimized for fast lookups (Set). + More memory-efficient than using an array with the `includes` method. **Library and Purpose** In both test cases, the library used is JavaScript's built-in Array and Set prototypes. The purpose of these libraries is to provide convenient methods for manipulating arrays and checking if elements exist within them. **Special JS Feature or Syntax (None)** There are no special JavaScript features or syntax being tested in this benchmark. **Other Considerations** When choosing between these two approaches, consider the trade-off between simplicity, performance, and memory usage. If you need to check if an element exists in a small array, the `includes` method might be sufficient. However, for larger arrays, creating a Set and using the `has` method can provide better performance. **Alternatives** Other alternatives for checking if an element exists in an array include: * Using a custom function with a linear search algorithm. * Using a data structure like a hash table or trie. * Using a library like Lodash or Ramda, which provides more advanced array manipulation functions. It's worth noting that the `includes` method has become a standard part of JavaScript in recent versions (ECMAScript 2019+), and is generally considered a good choice for most use cases. However, if performance or memory efficiency are critical, using a Set-based approach can provide benefits.
Related benchmarks:
native intersect vs lodash intersection
native intersect vs lodash intersect small and large 2
native intersect vs lodash intersect small and large 3
lodash intersectionWith vs array filtering with includes
Comments
Confirm delete:
Do you really want to delete benchmark?