Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set VS Array VS Object Dictionary
(version: 0)
Set vs Array vs Object Dictionary
Comparing performance of:
Set vs Array vs Obj
Created:
one year ago
by:
Guest
Jump to the latest result
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); var moveSlugObj = moveSlugs.reduce((a, c) => { return {...a, [c]:true} }, {});
Tests:
Set
moveSlugSet.has(move);
Array
moveSlugs.includes(move);
Obj
moveSlugObj[move]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
Array
Obj
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
86591024.0 Ops/sec
Array
37740080.0 Ops/sec
Obj
139317792.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks and explore what's being tested on MeasureThat.net. **Benchmark Definition JSON** The benchmark is defined by two parts: `Script Preparation Code` and `Html Preparation Code`. The script preparation code defines how to set up the test data, which consists of a list of slug values (`moveSlugs`). This list is then used to create two different data structures: 1. A `Set` called `moveSlugSet`, which contains unique slug values. 2. An object dictionary called `moveSlugObj`, where each key-value pair represents a slug value mapped to `true`. **Individual Test Cases** There are three test cases, each corresponding to one of the data structures created in the script preparation code: 1. **Set**: `moveSlugSet.has(move)`. This test case checks if a given slug value (`move`) exists as an element in the `moveSlugSet` set. 2. **Array**: `moveSlugs.includes(move)`. This test case checks if a given slug value (`move`) is present in the `moveSlugs` array. 3. **Obj**: `moveSlugObj[move]`. This test case attempts to access the value associated with a given slug key (`move`) in the `moveSlugObj` object dictionary. **Options Compared** The three test cases compare the performance of different data structure operations: * Sets (using `has()` method) * Arrays (using `includes()` method) * Object dictionaries (using array indexing) **Pros and Cons of Each Approach** Here's a brief summary of each approach: 1. **Sets**: Pros: * Fast lookups with an average time complexity of O(1). * Good for unique elements or fast membership testing. Cons: * Not designed for sequential access or random element retrieval. 2. **Arrays**: Pros: * Sequential access and indexing are fast (O(1)). * Suitable for ordered data or array-based operations. Cons: * Time complexity for `includes()` method is O(n) in the worst case, where n is the length of the array. 3. **Object Dictionaries**: Pros: * Fast lookups with an average time complexity of O(1), similar to sets. * Suitable for fast membership testing and sequential access (using object keys as indices). Cons: * Not designed for random element retrieval or ordered data. **Library Usage** None of the test cases use any external libraries. The `Set` implementation is a built-in JavaScript construct, while arrays and object dictionaries are also native JavaScript data structures. **Special JS Features or Syntax** No special JavaScript features or syntax are used in these benchmark definitions. They focus on showcasing the performance differences between sets, arrays, and object dictionaries for membership testing and lookup operations. **Other Alternatives** For those interested in exploring alternative data structures or implementations: * **Linked Lists**: An ordered data structure where each node points to the next element. * **Hash Tables**: A data structure optimized for fast lookups using hashing. * **Tries**: A data structure used for fast prefix matching and membership testing. These alternatives might provide different performance characteristics or use cases, but they are not directly relevant to this specific benchmark.
Related benchmarks:
Reduce Object.assign vs spread vs Map vs Function assignment
Object spread vs New map
Object spread vs New map with string keys
Reduce vs Assign
test_spread_vs-map
Comments
Confirm delete:
Do you really want to delete benchmark?