Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
dict vs set
(version: 0)
dict vs set
Comparing performance of:
set vs dict
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id=''></div>
Script Preparation code:
var dict = {} var set = new Set() var query = [] var n = 10000 for (var i = 0; i < n; i++) { dict[i] = true; set.add(i) query.push(Math.floor(Math.random() * n * 2)) query.push(Math.floor(Math.random() * n * 2)) }
Tests:
set
query.map(function(i) { return set.has(i) } )
dict
query.map(function(i) { return dict[i] } )
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set
dict
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
1771.8 Ops/sec
dict
3942.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested in the benchmark. **Benchmark Definition** The benchmark defines two variables: `dict` (an object) and `set` (a Set data structure). The script preparation code creates these variables, initializes them with 10,000 values, and populates them with random values. Additionally, an array `query` is created with 20 random values each. **Options Compared** The benchmark compares the performance of two approaches: 1. **Using a Dictionary (`dict`)**: The `query.map()` function is used to iterate over the dictionary and check if a value exists using the `in` operator (`i in dict`). This approach uses object lookup. 2. **Using a Set (`set`)**: The `query.map()` function is used to iterate over the set and check if a value exists using the `has()` method. This approach uses fast membership testing. **Pros and Cons** 1. **Dictionary (dict)** * Pros: + Easy to implement for developers familiar with JavaScript objects. + Can be useful when working with object keys that are not numerical indices. * Cons: + Object lookup can be slower than Set membership testing, especially for large datasets. + May involve more memory allocation and garbage collection due to the dynamic nature of object creation. 2. **Set** * Pros: + Fast membership testing with an average time complexity of O(1). + Efficient use of memory, as Sets only store unique values. * Cons: + Requires knowledge of Set data structures and their implementation. + May not be suitable for use cases that require complex object key-value relationships. **Library and Purpose** No specific library is mentioned in the benchmark definition. However, it's worth noting that the `Set` data structure is a built-in part of JavaScript, introduced in ECMAScript 2015 (ES6). **Special JS Feature or Syntax** None are mentioned explicitly in this benchmark. However, the use of `map()` and `in` operator (`i in dict`) are standard JavaScript syntax. **Other Alternatives** If you need to compare performance between a Dictionary and another data structure like an Array or a Linked List, you can modify the benchmark definition accordingly. For example: * Using an array instead of a dictionary: ```javascript var arr = []; for (var i = 0; i < n; i++) { arr.push(Math.floor(Math.random() * n * 2)); } ``` * Using a linked list instead of a set: ```javascript var LinkedList = function() {}; LinkedList.prototype.add = function(value) { // implementation of adding an element to the end of the list }; var list = new LinkedList(); for (var i = 0; i < n; i++) { list.add(i); } ``` Keep in mind that these alternatives will require additional code and may not accurately represent real-world scenarios where performance comparisons are crucial.
Related benchmarks:
Array.from vs Spread. But with one thousand sequential elements and with one thousand random elements from zero and one thousand.
List vs Dict
uniq indexOf vs lodash uniq
loadash vs set eg2
Comments
Confirm delete:
Do you really want to delete benchmark?