Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes vs obj[key] vs map.get 2
(version: 0)
Comparing performance of:
includes vs hash vs set vs map
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function shuffle(array) { let currentIndex = array.length, randomIndex; // While there remain elements to shuffle... while (currentIndex != 0) { // Pick a remaining element... randomIndex = Math.floor(Math.random() * currentIndex); currentIndex--; // And swap it with the current element. [array[currentIndex], array[randomIndex]] = [ array[randomIndex], array[currentIndex]]; } return array; } var a = shuffle(Array(100000).fill(undefined).map((_, i) => i)); var b = a.reduce((acc, el) => { acc[el] = true; return acc }, {}); var c = new Set(a); var d = new Map(a.map(el => [el, true])); var elements = [1, 100, 1000, 24, 56, 78, 23, 5643, 863, 2679, 9999, 45, 5832, 7510, 3756];
Tests:
includes
return elements.map(el => a.includes(el))
hash
return elements.map(el => b[el])
set
return elements.map(el => c.has(el))
map
return elements.map(el => d.get(el))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
includes
hash
set
map
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 JSON and explain what is tested on the website, along with the pros and cons of different approaches. **Benchmark Definition** The benchmark defines four test cases: 1. `set.has` 2. `array.includes` 3. `obj[key]` (assuming an object `a` created using the `shuffle` function) 4. `map.get` These tests compare the performance of four different data structures and methods to check for membership in a list: Sets, arrays, objects, and Maps. **Data Structures and Methods** Here's what each test case checks: 1. `set.has`: Checks if an element is present in a Set. 2. `array.includes`: Checks if an element is present in an array using the `includes` method. 3. `obj[key]`: Checks if an element is present in an object by accessing its key-value pair using bracket notation (`obj[key]`). (Note: This assumes that the object `a` has been shuffled and populated with unique elements.) 4. `map.get`: Checks if an element is present in a Map. **Options Compared** The benchmark compares the performance of these four approaches: * **Sets**: A data structure that stores unique values. It provides fast membership testing. * **Arrays**: A data structure that stores ordered collections of values. The `includes` method is used for membership testing. * **Objects**: A data structure that stores key-value pairs. Bracket notation (`obj[key]`) is used to access elements by their keys. * **Maps**: A data structure that stores key-value pairs with fast lookup times. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: 1. **Sets**: * Pros: Fast membership testing, good cache locality. * Cons: May not be suitable for all use cases (e.g., when elements need to be ordered or have multiple keys). 2. **Arrays**: * Pros: Easy to implement and understand, good for ordered data. * Cons: Membership testing can be slower than Sets due to the `includes` method's implementation. 3. **Objects**: * Pros: Convenient for accessing elements by their names (keys), easy to implement. * Cons: Slow membership testing due to bracket notation's overhead. 4. **Maps**: * Pros: Fast lookup times, convenient access to values using keys. * Cons: May not be suitable for all use cases (e.g., when elements need to be ordered). **Library and Special Features** The benchmark uses the `shuffle` function from the Lodash library to shuffle an array of unique elements. It also assumes that the object `a` has been created using this shuffled array. No special features or syntax are used in this benchmark. **Alternatives** If you're looking for alternatives to these approaches, consider: * Using a `Set` data structure instead of an array for fast membership testing. * Implementing your own `includes` method on arrays or objects for better performance. * Using a library like Lodash or Ramda that provides optimized implementations of these functions. Keep in mind that the choice of approach depends on your specific use case and requirements.
Related benchmarks:
Already sorted versus random
Array.Sort vs Math.Min-Max
shuffle array [dsng-manscaped]
reduce vs map & filter
Comments
Confirm delete:
Do you really want to delete benchmark?