Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.add vs array.push vs map.set
(version: 0)
Comparing performance of:
conditional array.push vs set.add vs map.set overwrite
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; var set = new Set(); var map = new Map(); var existingItem1 = {}; var existingItem2 = {}; var existingItem3 = {}; var newItem1 = {}; var newItem2 = {}; var newItem3 = {}; for (let i = 0, item; i < 100; i++) { item = {index: i}; array.push(item); set.add(item); map.set(item, 0); } array.push(existingItem1, existingItem2, existingItem3); set.add(existingItem1); set.add(existingItem2); set.add(existingItem3); map.set(existingItem1, 0); map.set(existingItem2, 0); map.set(existingItem3, 0);
Tests:
conditional array.push
if (!array.includes(existingItem1)) { array.push(existingItem1); } if (!array.includes(newItem1)) { array.push(newItem1); } if (!array.includes(existingItem2)) { array.push(existingItem2); } if (!array.includes(newItem2)) { array.push(newItem2); } if (!array.includes(existingItem3)) { array.push(existingItem3); } if (!array.includes(newItem3)) { array.push(newItem3); }
set.add
set.add(existingItem1); set.add(newItem1); set.add(existingItem2); set.add(newItem2); set.add(existingItem3); set.add(newItem3);
map.set overwrite
map.set(existingItem1, 0); map.set(newItem1, 0); map.set(existingItem2, 0); map.set(newItem2, 0); map.set(existingItem3, 0); map.set(newItem3, 0);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
conditional array.push
set.add
map.set overwrite
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:146.0) Gecko/20100101 Firefox/146.0
Browser/OS:
Firefox 146 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
conditional array.push
2721112.2 Ops/sec
set.add
10245503.0 Ops/sec
map.set overwrite
6837247.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the benchmark test cases provided. **Benchmark Description** The benchmark is comparing three different methods to add items to a collection: `array.push`, `set.add`, and `map.set`. The test case generates 100 unique items and then adds an additional 3 existing items (existingItem1, existingItem2, existingItem3) to the collection using each of these methods. The benchmark measures how many executions per second each method can handle. **Test Cases** There are three individual test cases: 1. **"conditional array.push"**: This test case uses an `if` statement to check if an item already exists in the array before pushing it. If it doesn't exist, it's added to the array. 2. **"set.add"**: This test case simply adds existing items and new items to a Set data structure using the `add` method. 3. **"map.set overwrite"**: This test case sets values for existing items in a Map data structure using the `set` method, effectively overwriting any previous value. **Library/Feature Used** None of these test cases use any external libraries or special JavaScript features beyond what's built-in to modern browsers (e.g., Sets and Maps). **Options Compared** The benchmark is comparing three different methods: * **array.push**: Adding items directly to an array using the `push` method. * **set.add**: Using a Set data structure with the `add` method to add unique items. * **map.set**: Using a Map data structure with the `set` method to store key-value pairs, effectively allowing overwriting of previous values. **Pros/Cons** Here are some pros and cons for each method: * **array.push**: + Pros: Simple implementation, fast array access and manipulation. + Cons: May have poor cache locality if items are sparse or not stored contiguously. * **set.add**: + Pros: Efficient storage of unique items, fast lookup times (average O(1)). + Cons: Not suitable for storing non-unique data or values. * **map.set**: + Pros: Allows efficient storage and retrieval of key-value pairs with average time complexity of O(1). + Cons: May incur overhead due to hash computation and collision resolution. **Other Considerations** When choosing an implementation, consider the specific use case: * If you need to store a large number of unique items and fast lookup is essential (e.g., in a cache or for fast membership testing), a Set might be a good choice. * For storing key-value pairs where overwriting existing values is acceptable, a Map could be suitable. * When working with arrays where cache locality and simple iteration are important (e.g., iterating over an array of items), `array.push` may be the way to go. **Alternatives** Other alternatives for similar use cases include: * Using a **Hash Table**, which can provide average time complexity of O(1) for insertions, deletions, and lookups. * Employing a more advanced data structure like a **Trie** or **Prefix Tree**, suitable for storing complex hierarchies or strings with efficient lookup and insertion. That's it! This explanation should help you understand the benchmark test cases provided.
Related benchmarks:
set.add vs array.push Fabien
set.add vs array.push vs map.set fork42
new Map vs set array to map
set.add vs array.push vs map.set
Comments
Confirm delete:
Do you really want to delete benchmark?