Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array lookup
(version: 0)
Comparing performance of:
array vs set
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Tests:
array
arr.includes(5)
set
const set = new Set(arr) set.has(5)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
array
set
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/136.0.0.0 Safari/537.36
Browser/OS:
Chrome 136 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
128832632.0 Ops/sec
set
9381509.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark definition is a JSON object that describes two test cases: 1. `arr.includes(5)` 2. `const set = new Set(arr) set.has(5)` These tests are comparing two approaches to check if a value exists in an array (or a set). **Options Compared** The options being compared are: * Array lookup using the `includes()` method * Using a set data structure with the `has()` method **Pros and Cons of Each Approach** **Array Lookup using includes()** Pros: * Simple and straightforward implementation * Widely supported by modern browsers * Can be used for various array operations beyond just existence checks Cons: * May have performance overhead due to the search algorithm (linear scan) * May not be suitable for very large arrays or datasets **Set Data Structure with has()** Pros: * Fast lookups (constant time complexity) since sets use hash tables internally * Suitable for large datasets and can improve performance in some cases Cons: * Requires additional memory to store the set * Implementation may vary depending on the programming language or environment (e.g., JavaScript engines) **Library: Set** In this benchmark, the `Set` library is used to create a set from an array. A set data structure is a collection of unique values that are ordered in no particular order. The purpose of using a set here is to demonstrate the trade-offs between array lookup and set-based lookups. While sets provide fast lookups, they require additional memory to store the set, which might be a concern for very large datasets. **Special JS Feature/Syntax** There are no special JavaScript features or syntax mentioned in this benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, here are some options: * Using other data structures like `Map` (JavaScript's built-in Map object) or custom hash tables * Implementing your own search algorithm for arrays * Using a different programming language or environment that may provide better performance or features Keep in mind that the choice of approach depends on the specific requirements and constraints of your project.
Related benchmarks:
testing--js
Array.from(new Set(array)) vs [...new Set(array)]
filtering with Array.prototype.flatMap vs for of loop
filtering with Array.prototype.flatMap vs for of loop vs Array.prototype.map + Array.prototype.filter
Indexof vs Includes in an array
Comments
Confirm delete:
Do you really want to delete benchmark?