Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array vs Set for removing value
(version: 0)
Comparing performance of:
Set vs Array
Created:
5 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var master = Array(100).fill(null).map((a, b) => b); var a = [...master]; var s = new Set(master);
Tests:
Set
master.forEach(x => s.delete(x));
Array
master.forEach(x => a.splice(a.indexOf(x), 1));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Array
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Set
1653146.5 Ops/sec
Array
352228.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases. **Benchmark Definition:** The benchmark is designed to compare two approaches for removing values from an array or set in JavaScript: using `Set` data structure and using `Array.splice()` method. **Script Preparation Code:** The script preparation code creates a master array with 100 elements, filled with null values, and maps over the array to create another array (`a`) that contains only the index values of the original array. A new set (`s`) is created from the master array. **Html Preparation Code:** There is no HTML preparation code provided for this benchmark. **Test Cases:** 1. **"Set"`** The test case executes the `master.forEach(x => s.delete(x));` statement, which removes all values from the set `s`. 2. **"Array"`** The test case executes the `master.forEach(x => a.splice(a.indexOf(x), 1));` statement, which removes all values from the array `a` using the `splice()` method. **Libraries and Frameworks:** * None are explicitly mentioned in the provided benchmark definition. **Special JS Features or Syntax:** There is no special JavaScript feature or syntax used in this benchmark. It only relies on standard JavaScript functionality. **Pros and Cons of Different Approaches:** 1. **Using `Set` data structure (`"Set"` test case):** * Pros: + Sets are generally more efficient than arrays for removing elements, especially when dealing with unique values. + Sets provide a faster lookup time compared to array indices. * Cons: + Sets are not suitable for situations where preserving the original array order is necessary. 2. **Using `Array.splice()` method (`"Array"` test case):** * Pros: + Preserves the original array order, which might be desirable in some cases. + Does not require creating a new data structure (like sets do). * Cons: + Splicing an array can be slower than removing elements from a set for large datasets. **Other Alternatives:** 1. For arrays, other alternatives to `splice()` could include using `filter()`, `map()`, or `reduce()`, depending on the specific use case. 2. For sets, alternative data structures that might offer better performance in certain scenarios include `WeakSet` (for set operations without retaining a reference to elements) or custom implementations using other data structures like binary search trees. Keep in mind that these alternatives may come with their own trade-offs and optimizations, depending on the specific requirements of the use case.
Related benchmarks:
Array vs Set vs Object for removing value
Array vs Set vs Object for removing value (1000 entries)
Array vs Set (from Array) vs Object for removing value
set delete vs array splice
Comments
Confirm delete:
Do you really want to delete benchmark?