Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs Objects vs Array
(version: 0)
Comparing performance of:
includes vs Object vs Set
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var set = new Set(); var obj = {}; var array = []; var size = 1000000; var test = (function (length, size, probability) { function random() { return Math.random().toString(36).substr(2); } var i; for (i = 0; i < size; i++) { var str = random(); set.add(str); obj[str] = true; array[i] = str; } var test = []; test.length = length; var c = size / probability; for (i = 0; i < length; i++) { var index = Math.floor(Math.random() * c); test[i] = index < size ? array[index] : random(); } return test; })(10000, 0.5);
Tests:
includes
var count = 0; for (var i = 0; i < test.length; i++) { if (array.includes(test[i])) { count++; } }
Object
var count = 0; for (var i = 0; i < test.length; i++) { if (!!test[i]) { count++; } }
Set
var count = 0; for (var i = 0; i < test.length; i++) { if (set.has(test[i])) { count++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
Object
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 Overview** The benchmark tests three different data structures: Sets, Objects, and Arrays. The goal is to measure which data structure has the fastest lookup time when searching for an existing value. **Options Compared** 1. **Set**: A Set is a collection of unique values that can be used to store and retrieve values efficiently. 2. **Object**: An Object is a collection of key-value pairs, where each key is unique and maps to a specific value. 3. **Array**: An Array is an ordered collection of values that can be accessed by index. **Pros and Cons** 1. **Set**: * Pros: Fast lookup time (O(1) on average), no duplicates, easy to create. * Cons: May have slower creation times compared to other data structures. 2. **Object**: * Pros: Can store complex data types, flexible key-value pairs. * Cons: May have slower lookup times due to the need to hash keys, and may not be as efficient for large datasets. 3. **Array**: * Pros: Fast access by index, easy to manipulate. * Cons: Slower lookup time compared to Sets or Objects (O(n)), and may require shifting elements when inserting or removing items. **Library Used** None is explicitly mentioned in the benchmark definition or test cases. However, it's likely that the JavaScript engine being used provides built-in support for these data structures. **Special JS Features/Syntax** The benchmark uses several advanced JavaScript features: 1. **Arrow functions**: Used to create concise and expressive functions. 2. **Template literals**: Used to create string literals with placeholders for values. 3. **Array methods**: Used to manipulate arrays, such as `includes()` and `has()`. 4. **Variable declarations with let/const**: Used to declare variables that are scoped to the block. **Other Alternatives** 1. **Hash Tables**: Similar to Sets, but may have slower lookup times due to the need to hash keys. 2. **Associative Arrays**: Data structures like JavaScript objects or maps can be used for faster key-value lookups. 3. **Binary Search Trees**: Can be used for efficient search and insertion of values. Overall, the benchmark provides a useful comparison of three common data structures in JavaScript, highlighting their strengths and weaknesses in different scenarios.
Related benchmarks:
Set.prototype.has() vs "in" operator
Set vs Objects vs Array 2
Sets, Objects, Arrays Performance
JavaScript spread operator vs Object.assign performance, large objects v2
Comments
Confirm delete:
Do you really want to delete benchmark?