Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sets, Objects, Arrays Performance
(version: 1)
Comparing performance of:
Set vs Object vs Array
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var set = new Set(); var object = Object.create(null); var array = []; var size = 1000; array.length = size; var test = (function (length, probability) { function random() { return Math.random().toString(36).substr(2); } var i; for (i = 0; i < size; i++) { var str = random(); set.add(str); object[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:
Set
var count = 0; for (var i = 0; i < test.length; i++) { if (set.has(test[i])) { count++; } }
Object
var count = 0; for (var i = 0; i < test.length; i++) { if (!!object[test[i]]) { count++; } }
Array
var count = 0; for (var i = 0; i < test.length; i++) { if (array.includes(test[i])) { count++; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
Object
Array
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):
I'll dive into explaining the provided JSON benchmark definition and test cases. **Benchmark Definition** The benchmark measures the performance of three data structures: Sets, Objects, and Arrays in JavaScript. The goal is to determine which data structure performs better when iterating over its elements and checking for existence or membership. **Options Compared** There are two main approaches being compared: 1. **Sets**: Using a `Set` object from the JavaScript built-in library, which provides an efficient way to store unique values. 2. **Objects**: Using the `Object.create(null)` method to create an object with no prototype chain, allowing for fast lookups using bracket notation (`object[test[i]]`). 3. **Arrays**: Using a standard array (`array`) and checking membership using the `includes()` method. **Pros and Cons of Each Approach** 1. **Sets**: * Pros: Fast lookup times (average O(1)), efficient memory usage. * Cons: May have higher overhead for creating and manipulating sets, as they require more memory than objects or arrays to store unique values. 2. **Objects**: * Pros: Fast lookups using bracket notation (`object[test[i]]`), can be more flexible than arrays for custom data structures. * Cons: May have slower lookup times compared to sets, especially for large datasets, due to the overhead of creating and managing objects. 3. **Arrays**: * Pros: Easy to use and understand, fast membership testing using `includes()`. * Cons: Slower lookups than sets or objects (average O(n)), may have higher memory usage depending on the data distribution. **Library and Purpose** The `Set` object is a built-in JavaScript library that provides an efficient way to store unique values. Its purpose is to provide fast lookup, insertion, and removal operations for elements. **Special JS Feature/Syntax** None mentioned in this benchmark definition. **Other Considerations** * The benchmark uses a fixed dataset size (`size = 1000`) and a random probability (`probability = 0.5`), which may not accurately represent real-world scenarios. * The `TestName` field in each test case indicates the name of the individual test case, but it doesn't provide any additional context or information about the specific scenario being tested. **Alternatives** For similar benchmarks, you can consider using other data structures like: 1. **Maps**: Similar to objects, but with key-value pairs and more flexible lookup methods. 2. **Linked Lists**: A dynamic data structure that can be used for efficient insertion and deletion operations. 3. **Hash Tables**: A data structure optimized for fast lookups, insertions, and deletions. Keep in mind that the choice of data structure depends on the specific use case and requirements of your application.
Related benchmarks:
Fill array with random integers
array vs int32Array
array vs int16array
array vs int16array try catch
array vs float64 for io and slice (fixed)
Comments
Confirm delete:
Do you really want to delete benchmark?