Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Array cuttle
(version: 0)
Checking to see if a variable is included
Comparing performance of:
Set vs array
Created:
one year ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id='1'></div>
Script Preparation code:
var move = 'pass'; var moveSlugs = [ 'draw', 'points', 'faceCard', 'scuttle', 'untargetedOneOff', 'targetedOneOff', 'jack', 'counter', 'resolve', 'resolveThree', 'resolveFour', 'resolveFive', 'seven/points', 'seven/scuttle', 'seven/faceCard', 'seven/jack', 'seven/untargetedOneOff', 'seven/targetedOneOff', 'pass', ]; var moveSlugSet = new Set(moveSlugs);
Tests:
Set
moveSlugSet.has(move);
array
moveSlugs.includes(move);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:129.0) Gecko/20100101 Firefox/129.0
Browser/OS:
Firefox 129 on Ubuntu
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
723770944.0 Ops/sec
array
20758892.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and their pros and cons. **Benchmark Context** The benchmark is designed to compare two approaches for checking if a variable `move` exists in an array or a Set data structure. The goal is to determine which approach is faster and more efficient. **Script Preparation Code** The script prepares two variables: 1. `moveSlugs`: an array of strings containing various values. 2. `moveSlugSet`: a Set created from the `moveSlugs` array using the `Set()` constructor. **Html Preparation Code** The HTML code simply creates a `<div>` element with an ID of "1". **Benchmark Test Cases** There are two test cases: 1. **"Set"`: The benchmark calls the `has()` method on the `moveSlugSet` Set instance, passing the `move` variable as an argument. 2. **"array"`: The benchmark calls the `includes()` method on the `moveSlugs` array, passing the `move` variable as an argument. **Comparison** The two approaches are compared in terms of their execution time and performance. **Pros and Cons of each approach:** 1. **Set (`has()` method)**: * Pros: + Faster lookup times (O(1) average case). + More memory-efficient for large datasets. * Cons: + Less flexible than array-based approaches, as it only supports exact value matches. 2. **Array (`includes()` method)**: * Pros: + More flexible and forgiving than Set-based approaches, as it allows for partial value matches and case-insensitive matching. * Cons: + Slower lookup times (O(n) average case), especially for large datasets. **Special JS Features/Syntax** The benchmark uses the following JavaScript features: 1. **Sets**: Introduced in ECMAScript 2015, Sets are a data structure that allows for efficient membership testing and fast lookups. 2. **Array methods**: The `includes()` method is a modern array method introduced in ECMAScript 2019. **Other Considerations** When choosing between these approaches, consider the following factors: 1. **Data size and complexity**: For large datasets or complex data structures, Set-based approaches might be more suitable. 2. **Performance requirements**: If speed is critical, the Set-based approach (`has()` method) might be a better choice. 3. **Flexibility and forgiveness**: If you need to match values with slight variations or allow for partial matches, the array-based approach (`includes()` method) might be more suitable. **Alternative Approaches** If neither of these approaches meets your requirements, consider the following alternatives: 1. **Using a Map data structure**: Maps can provide fast lookups and flexibility similar to Sets. 2. **Implementing a custom lookup function**: You could write a custom function to perform the lookup, allowing you to optimize it for specific use cases. Keep in mind that these alternative approaches may have different trade-offs in terms of performance, memory usage, and code complexity.
Related benchmarks:
push vs spread for single element addition to array
Add new array to array: push vs destructuring
Push & Shift vs Spread & Pop
Push & Shift vs Spread & Pop (2)
array.from.map vs array.from with map
Comments
Confirm delete:
Do you really want to delete benchmark?