Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Access Object, Map, Array, Set with n items
(version: 0)
Comparing performance of:
Get from Object vs Get from Map vs Has key in Map vs Has key in Array OLD vs Has key in Array NEW vs Has key in Set
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var length = 10000; var key = '5000'; var obj = Array.from({length}).reduce((obj,_,index) => obj[index.toString()] = index, {}); var map = new Map(Array.from({length}).map((_,index) => [index.toString(), index])); var arr = Array.from({length}).map((_,index) => index.toString()); var set = new Set(Array.from({length}).map((_,index) => index.toString()));
Tests:
Get from Object
var value = obj[key];
Get from Map
var value = map.get(key);
Has key in Map
var has = map.has(key);
Has key in Array OLD
var has = arr.indexOf(key) > -1;
Has key in Array NEW
var has = arr.includes(key);
Has key in Set
var has = set.has(key);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
Get from Object
Get from Map
Has key in Map
Has key in Array OLD
Has key in Array NEW
Has key in Set
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 benchmark and its test cases. **Benchmark Definition** The benchmark definition is a JSON object that describes the script preparation code for creating a set of data structures: an object, a map, an array, and a set. The script creates 10,000 items with unique keys and values, and populates each data structure accordingly. ```json "Script Preparation Code": "var length = 10000;\r\nvar key = '5000';\r\nvar obj = Array.from({length}).reduce((obj,_,index) => obj[index.toString()] = index, {});\r\nvar map = new Map(Array.from({length}).map((_,index) => [index.toString(), index]));\r\n\r\nvar arr = Array.from({length}).map((_,index) => index.toString());\r\nvar set = new Set(Array.from({length}).map((_,index) => index.toString())); ``` **Test Cases** The benchmark consists of six test cases, each measuring the performance of a different operation: 1. **Get from Object**: Measures the time it takes to retrieve a value from the object using its key. 2. **Get from Map**: Measures the time it takes to retrieve a value from the map using its key. 3. **Has key in Map**: Measures the time it takes to check if a key exists in the map. 4. **Has key in Array OLD** (deprecated): Measures the time it takes to check if a key exists in the array using its `indexOf` method. (Note: This method is deprecated and has been replaced by `includes`). 5. **Has key in Array NEW**: Measures the time it takes to check if a key exists in the array using its `includes` method. 6. **Has key in Set**: Measures the time it takes to check if a key exists in the set. **Library and Special Features** * The `Map` object is used to create the map data structure. The `Map` API provides methods for accessing and manipulating key-value pairs, such as `get`, `has`, and `set`. * The `Set` object is used to create the set data structure. The `Set` API provides methods for adding and removing elements, as well as checking if an element exists. * The `includes` method was introduced in ECMAScript 2019 (ES10) as a replacement for the deprecated `indexOf` method. **Options Compared** The benchmark compares the performance of different operations on these data structures: * **Object**: Measures access time using its key. * **Map**: Measures access time using its key. * **Array (OLD)**: Measures access time using its `indexOf` method. (Note: This method is deprecated and has been replaced by `includes`). * **Array (NEW)**: Measures access time using its `includes` method. * **Set**: Measures access time using its `has` method. **Pros and Cons** Each approach has its own pros and cons: * **Object**: Fast access times, but may require more memory due to the object's inherent structure. * **Map**: Efficient key-value storage and retrieval, with fast access times. * **Array (OLD)**: Slow access times compared to modern methods like `includes`. * **Array (NEW)**: Modern method for checking if an element exists in an array. * **Set**: Fast membership testing, but may not be as efficient for large datasets. **Other Alternatives** If you need to measure the performance of other data structures or operations, consider the following alternatives: * **Hash Table**: Similar to `Map`, but with a different implementation and syntax. (Not implemented in this benchmark) * **Trie**: Data structure optimized for fast lookup and prefix matching. * **Doubly Linked List**: Data structure optimized for efficient insertion, deletion, and traversal. Keep in mind that the performance characteristics of these alternatives may vary depending on your specific use case and requirements.
Related benchmarks:
Access Object, Map, Array, Set with n items #2
Object.keys().length vs Map.size
Test length assign 100k
reduce object vs object.fromentries (1K records)
Comments
Confirm delete:
Do you really want to delete benchmark?