Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Comparing and Filtering two arrays, using the set.has vs. array.includes vs array.indexOf
(version: 3)
Comparing performance of:
includes vs lookup vs indexOf
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var a = Array.from({ length: 1000 }, (_, v) => v); var temp = [400, 600, 700, 800]; var b = new Set(a)
Tests:
includes
return temp.map(it => a.includes(it));
lookup
return temp.map(it => b.has(it));
indexOf
return temp.map(it => a.indexOf(it) !== -1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
lookup
indexOf
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 dive into the explanation of the provided benchmark. **What is tested:** The provided JSON represents a JavaScript microbenchmark that tests three different approaches to compare and filter two arrays: 1. `includes`: Using the `array.includes()` method to check if an element exists in the array. 2. `lookup`: Using the `set.has()` method to look up if an element exists in the set (which is created by converting the array to a set). 3. `indexOf`: Using the `array.indexOf()` method to find the index of the first occurrence of an element in the array. **Options compared:** The three approaches are compared in terms of their performance, which is measured by the number of executions per second. **Pros and Cons of each approach:** 1. **includes**: * Pros: Simple and widely supported, easy to implement. * Cons: May have higher overhead due to creating a new array for searching, especially for large datasets. 2. **lookup** (using `set.has()`): * Pros: Fast lookup time, as sets are designed for efficient membership testing. * Cons: Requires converting the array to a set, which may have additional memory overhead and can be slower than arrays for small datasets. 3. **indexOf**: * Pros: Can return the index of the element if found, which can be useful in some cases. * Cons: May have higher overhead due to searching the entire array, especially for large datasets. **Other considerations:** * The benchmark uses a random dataset (1000 elements) created using `Array.from()`, which ensures that the results are not biased towards smaller or larger datasets. * The `set` is created from the array using `new Set(a)`, which converts the array to a set for efficient membership testing. * The benchmark runs on a desktop platform with Chrome 111, which may have specific performance characteristics that affect the results. **Library used:** The `Set` data structure is used in the `lookup` test case. A `Set` is an unordered collection of unique values, which makes it ideal for efficient membership testing. **Special JS feature or syntax:** None are mentioned in the provided code snippets, but JavaScript has many other features and syntaxes that can affect performance, such as closures, async/await, Web Workers, etc. If these were used in the benchmark, they would be mentioned in the script preparation code. **Alternatives:** There are other approaches to compare elements in an array or set, such as: * Using `array.indexOf()` with a callback function (e.g., `a.indexOf(it => it > temp[0])`) * Using regular expressions (`temp.map(it => new RegExp(`^${it}$`).test(temp[0]))`) * Using `Set.prototype.has()` instead of `set.has()` * Using specialized libraries like `fast-assert` or `jest-matchers` for array and set operations. However, the provided benchmark using `includes`, `lookup`, and `indexOf` methods is a common and straightforward way to compare performance in JavaScript.
Related benchmarks:
Lodash difference vs JS filter and includes
array indexOf vs includes vs some using numbers
Set from array vs array Filter unique
set vs array find if exists v2
Comments
Confirm delete:
Do you really want to delete benchmark?