Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes on big arr
(version: 0)
Comparing performance of:
includes vs lookup vs transform to obj
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))
transform to obj
const obj1 = arr1.reduce((acc,val) => ({...acc, [val]: true }), {}) return arr2.filter(x => !!obj1[x])
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
lookup
transform to obj
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 is designed to measure the performance of three different approaches on large arrays: 1. `Array.includes()` 2. Creating an object from an array (`Object.fromEntries()` or equivalent) and using it as a lookup set with `has()` or `in` operator 3. Converting the array to an object using `reduce()` and then filtering it **Options Compared** The benchmark is testing three approaches: * Approach 1: `Array.includes()` + This method iterates through the array until it finds the specified element or reaches the end of the array. + Pros: - Simple to implement - Fast for small arrays + Cons: - Slow for large arrays due to linear search * Approach 2: Creating an object from an array and using `has()` or `in` operator + This method involves creating an object with the array elements as keys, which takes time and memory. + Pros: - Fast for large arrays since lookup is O(1) in JavaScript objects + Cons: - Requires extra memory allocation and object creation * Approach 3: Converting the array to an object using `reduce()` and then filtering it + This method involves creating a new object with the array elements as keys, which takes time and memory. + Pros: - Fast for large arrays since lookup is O(1) in JavaScript objects + Cons: - Requires extra memory allocation and object creation **Library: Object.fromEntries()** `Object.fromEntries()` is a method introduced in ECMAScript 2015 that creates an object from an iterable (such as an array) with the specified entries. In this benchmark, it's used to create an object from the `arr1` array, which serves as a lookup set for the `has()` or `in` operator. **Special JS Feature: Arrow Functions** The benchmark uses arrow functions (`() => { ... }`) in some of the test cases. These are a shorthand syntax for creating functions that don't require the `function` keyword. They're commonly used in modern JavaScript and provide concise syntax. **Benchmark Preparation Code** The preparation code creates two large arrays, `arr1` and `arr2`, using `Array.from()`. It then creates two sets from these arrays using `Set()` constructors. The final test cases use these sets to filter the original arrays. **Latest Benchmark Result** The benchmark result shows the execution frequency per second for each of the three approaches on a desktop Chrome 94 browser: * Approach 1: `Array.includes()`: 910 executions/second * Approach 2: Creating an object from an array and using `has()` or `in` operator: 2963 executions/second (slower than expected due to memory allocation) * Approach 3: Converting the array to an object using `reduce()` and then filtering it: 9279 executions/second (fastest) **Other Alternatives** Some alternative approaches that could be explored in a benchmark include: * Using `for...of` loop for iteration instead of `Array.includes()` * Implementing a custom lookup data structure, such as a hash table * Utilizing native Web Assembly (WASM) for performance-critical code
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 (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?