Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes for 1000 elements
(version: 0)
Comparing performance of:
includes vs lookup
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array(1000).fill(null).map((__, i) => i) var b = new Set(Array(1000).fill(null).map((__, i) => i))
Tests:
includes
return a.includes(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/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
116394240.0 Ops/sec
lookup
207915184.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of what's being tested in this benchmark. **Benchmark Definition** The provided JSON represents a JavaScript microbenchmark, specifically comparing two approaches: `array.includes` and `Set.has`. The benchmark aims to measure which method is faster for checking if an element (in this case, the number 9) exists within a large array or set of 1000 elements. **Options Compared** Two options are being compared: 1. **Array.includes**: This method checks if an element exists in the array by iterating through its length and comparing each element to the target value. 2. **Set.has**: This method uses a data structure called a Set, which is optimized for fast membership testing. When you call `has` on a Set, it essentially asks the question "do I have this element in my set?", which can be answered quickly by examining the internal data structure of the Set. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * **Array.includes**: + Pros: Simple to implement and widely supported. + Cons: Has to iterate through the entire array, making it slower for large datasets. * **Set.has**: + Pros: Much faster for membership testing, especially with large datasets. + Cons: Requires a Set data structure, which might not be familiar to all developers. In general, if you need to perform fast membership testing on large datasets, using a Set is the way to go. However, if simplicity and wide support are more important than raw performance, `array.includes` might still be the better choice. **Library Used** The benchmark uses the JavaScript built-in `Set` data structure, which is part of the ECMAScript standard (as of ECMAScript 2015). The Set data structure provides fast membership testing and other useful methods for working with collections of unique values. **Special JS Feature or Syntax** There's no special JavaScript feature or syntax being used in this benchmark. It's a straightforward comparison of two existing methods using the JavaScript language. **Other Alternatives** If you're looking for alternative approaches, here are a few: * For membership testing on large datasets, you could consider using a data structure like a `Map`, which is similar to a Set but provides additional lookup methods. * If you need to perform more complex membership tests (e.g., checking if an element exists in multiple arrays or sets), you might want to explore other libraries or frameworks that provide specialized functions for these tasks. Keep in mind that the choice of data structure and implementation will depend on your specific use case, performance requirements, and familiarity with JavaScript.
Related benchmarks:
set.has vs. array.includes 2
Includes (array) vs Has (Set)
set.has vs. array.includes on 25 000 items
Array.includes vs Set.has vas Map.has 2
Comments
Confirm delete:
Do you really want to delete benchmark?