Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes on big arrays sf
(version: 0)
Comparing performance of:
includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var arr1 = Array.from({ length: 1000 }, (_, i) => i + 1) var arr2 = Array.from({ length: 1000 }, (_, i) => i * 1000) var set1 = new Set(arr1) var set2 = new Set(arr2)
Tests:
includes
return arr2.filter(x => arr1.includes(x))
lookup
return [...set2].filter(x => set1.has(x))
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:
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 break down the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches to check if an element exists in an array or set: `arr.includes()` (or its equivalent with spread syntax, `[...set].has(x)`) versus checking if an element is present in the underlying collection of the set (`set1.has(x)`). **Options Compared** There are three options being compared: 1. **`arr2.filter(x => arr1.includes(x))`**: This approach uses `includes()` on the original array `arr1` and filters out elements that aren't found. 2. **`[...set2].filter(x => set1.has(x))`**: This approach uses the spread syntax to convert the set to an array, then uses `has()` on the original underlying collection of the set (`set1`). 3. **A third option is implied but not explicitly defined in the provided benchmark**: We can assume it would be similar to options 1 and 2. **Pros and Cons** Here are some pros and cons for each approach: 1. **`arr2.filter(x => arr1.includes(x))`**: * Pros: Simple, straightforward implementation. * Cons: May involve unnecessary iterations or lookups if `includes()` is slow. 2. **`[...set2].filter(x => set1.has(x))`**: * Pros: Uses the efficient underlying collection of the set for lookups, potentially faster than option 1. * Cons: Requires extra memory allocation and creation of an array using spread syntax. **Library Usage** The `Set` data structure is used in this benchmark. A `Set` is a collection of unique values that cannot be duplicated. Its main purpose is to provide fast lookup, insertion, and deletion operations. **Special JavaScript Features or Syntax** The benchmark uses the following special feature: * **Spread syntax (`[...set2]`)**: This syntax allows converting a set (or an array) into an array using a concise syntax. **Other Alternatives** If we were to consider alternative approaches for this benchmark, we might explore: * Using `map()` and checking if any elements match the target value. * Using a custom implementation of `includes()` or `has()`. * Comparing the performance with other languages or frameworks that have optimized set operations. However, without more context, it's difficult to provide a comprehensive alternative set of benchmarks.
Related benchmarks:
set.has vs. array.includes on big arrays s
Array.from vs. ... expansion
array.includes vs set.has for small-ish n
JS includes vs set
set.has vs. array.includes (find 999,999 in 1,000,000)
Comments
Confirm delete:
Do you really want to delete benchmark?