Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set<object> vs Array<object>
(version: 0)
Comparing performance of:
Set<object> vs Array<object>
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var objects = []; for(let i = 0; i < 100; i++) { objects.push({ id: i, x: 0, y: -100, str: "Test" }); } var arr = []; var set = new Set();
Tests:
Set<object>
for(let i = 0; i < 50; i++) { set.add(objects[i]); } for(let i = 0; i < 100; i++) { const x = set.has(objects[i]); } for(let i = 0; i < 100; i++) { set.delete(objects[i]); }
Array<object>
for(let i = 0; i < 50; i++) { const obj = objects[i]; arr[obj.id] = obj; } for(let i = 0; i < 100; i++) { const x = objects[i].id in arr; } for(let i = 0; i < 100; i++) { delete arr[objects[i].id]; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set<object>
Array<object>
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 provided JSON defines a JavaScript microbenchmark that compares two data structures: `Set` and `Array`. The benchmark is designed to measure the performance of adding, checking if an element exists in the set, and deleting elements from both data structures. **Script Preparation Code** The script preparation code creates arrays of 100 objects with properties `id`, `x`, `y`, and `str`. This code sets up a pool of objects that will be used to populate the `Set` and `Array`. **Options Compared** Two options are being compared: 1. **Set**: A `Set` is a collection of unique values, which allows for fast lookup, insertion, and deletion operations. 2. **Array**: An array is an ordered collection of values that can be indexed using bracket notation (`arr[obj.id]`). **Pros and Cons** Here are the pros and cons of each approach: * **Set**: + Pros: Fast lookup, insertion, and deletion operations (O(1) time complexity). + Cons: Requires extra memory to store unique values. * **Array**: + Pros: Familiar syntax for many developers, allows for indexing and array-like methods. + Cons: Lookup, insertion, and deletion operations are slower (O(n) time complexity). **Library Used** The `Set` data structure is built into JavaScript's standard library. Its purpose is to provide a fast and efficient way to store unique values. **Special JS Feature/Syntax** There doesn't seem to be any special JavaScript features or syntax used in this benchmark. **Other Alternatives** If you're interested in exploring alternative approaches, here are some options: * **Map**: A `Map` data structure is similar to an array but provides key-value pairs. It can be used for fast lookup and insertion operations (O(1) time complexity). * **Sorted Set**: Some libraries, like the one on npm called `sorted-set`, provide a sorted set data structure that allows for fast insertion, deletion, and searching (O(log n) time complexity). Keep in mind that these alternatives might require additional dependencies or have different performance characteristics. **Benchmark Result Interpretation** The latest benchmark result shows that the `Set` approach outperforms the `Array` approach. This is likely due to the optimized implementation of the `Set` data structure and its ability to handle unique values efficiently.
Related benchmarks:
set.add vs array.push
Array.from vs Spread declaring the Set
recreate array vs set 2
Create Set vs loop
Array push or set
Comments
Confirm delete:
Do you really want to delete benchmark?