Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set vs array find if exists
(version: 0)
Comparing performance of:
array vs set
Created:
2 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:
array
a.indexOf(7);
set
b.has(7);
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
array
59248796.0 Ops/sec
set
112207488.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the explanation of the provided JavaScript microbenchmark. **Benchmark Overview** The benchmark measures the performance difference between two approaches: using an array (`indexOf`) and using a `Set` data structure (`has`) to check if a specific value exists. The benchmark compares the execution time of these two approaches for searching for a specific element within each data structure. **Options Compared** There are two options being compared: 1. **Array `indexOf()`**: This method searches for the specified value in the array and returns its index if found, or -1 if not found. 2. **Set `has()`**: This method checks if the specified value is present in the set. **Pros and Cons of Each Approach** 1. **Array `indexOf()`**: * Pros: Simple to implement, widely supported. * Cons: O(n) time complexity, where n is the length of the array. This means that as the size of the array increases, the execution time of this method will also increase linearly. 2. **Set `has()`**: * Pros: Fast lookup time (average case O(1)), especially for large datasets. * Cons: Requires a set data structure to be created initially, which can consume memory. **Library and Its Purpose** In the provided benchmark, the `Set` library is used to create a set data structure (`b`) from an array (`a`). The `Set` object provides methods like `has()` for checking if a value is present in the set. The purpose of using a `Set` here is to demonstrate its performance advantages over array-based searching. **Special JS Feature or Syntax** The benchmark uses JavaScript's built-in `Set` data structure, which was introduced in ECMAScript 2015 (ES6). This feature allows for efficient and compact storage of unique values. The `has()` method is a part of the `Set` prototype and provides a fast way to check if a value is present in the set. **Other Alternatives** Besides using an array (`indexOf`) or a `Set` (`has`), other alternatives for searching for a specific element within an array or set include: * Using `Array.prototype.includes()` (introduced in ECMAScript 2015, ES6) instead of `indexOf()`. * Using a custom implementation with binary search. * Using other data structures like a hash table or a trie. However, these alternatives may not be as widely supported or efficient as the built-in `Set` and `has()` methods.
Related benchmarks:
set.has vs. array.includes - including extra code for more fair comparison as typically you will have an array and not a set
set vs array iteration
Includes (array) vs Has (Set)
set vs array find if exists v2
Comments
Confirm delete:
Do you really want to delete benchmark?