Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array vs Set (from Array) vs Object for removing value
(version: 0)
Comparing performance of:
Set vs Array vs Object
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var master = Array(1000).fill(null).map((a, b) => b); var a = [...master]; var o = []; master.forEach(x => { o[x] = x })
Tests:
Set
var s = new Set(master); master.forEach(x => s.delete(x));
Array
master.forEach(x => a.splice(a.indexOf(x), 1));
Object
master.forEach(x => delete o[x]);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Set
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):
**Benchmark Explanation** The provided benchmark measures the performance of three different data structures in removing values from an array: Sets, Arrays, and Objects. In this benchmark, a large array `master` is created with 1000 elements, filled with null values, and then mapped to create a new array `a`. Meanwhile, another variable `o` is initialized as an empty object. The three test cases are: 1. **Set**: A Set `s` is created from the original array `master`, and then each element is deleted using the `delete` method. 2. **Array**: The array `a` is modified by removing each element at its index using the `splice` method, which shifts all subsequent elements to fill the gap. 3. **Object**: The object `o` is modified by deleting each key-value pair where the value is equal to the corresponding element in the original array `master`, but only if it exists as a key. **Comparison of Approaches** 1. **Set**: This approach uses the `delete` method on the Set, which has an average time complexity of O(1) for removing elements. * Pros: Fast removals, efficient memory usage since Sets automatically remove duplicates and maintain uniqueness. * Cons: May require additional memory allocation when dealing with large datasets since Sets may need to store a reference count to ensure each element is unique. 2. **Array**: The array modification method `splice` uses an average time complexity of O(n), where n is the number of elements being removed, as it involves shifting all remaining elements to fill the gap. * Pros: More intuitive for operations like removing consecutive or specific values within the array, efficient memory reuse. * Cons: Can lead to slower execution times with large datasets due to shifting a larger number of elements. 3. **Object**: The object modification method `delete` uses an average time complexity similar to Set since it directly deletes the key-value pair without maintaining uniqueness automatically but may require extra iteration checks if values are used as keys. * Pros: Suitable when data already exists in a structured format with meaningful keys, allows for quick lookup and removal. * Cons: May not be optimal for performance in large datasets due to potential iteration overhead. **Library Used** No specific library is mentioned in the benchmark setup code provided. However, JavaScript's built-in `Set` data structure is utilized as part of the benchmark test cases. **Special JS Feature/Syntax** The benchmark tests the performance of methods that directly interact with built-in data structures (`Set`, `Array`, and `Object`) without requiring any external libraries or advanced syntax features like async/await or Promises.
Related benchmarks:
map vs forEach Chris
map vs forEach Chris v2b
Delete vs destructure for objects in loop
Array.forEach vs Object.keys().forEach
Array from() vs Map.keys()
Comments
Confirm delete:
Do you really want to delete benchmark?