Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.add vs array.push vs map.set fork42
(version: 0)
Comparing performance of:
conditional array.push vs set.add vs map.set overwrite
Created:
3 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
array.push(existingItem1); array.push(newItem1); array.push(existingItem2); array.push(newItem2); array.push(existingItem3); 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:
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 dive into the provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark is designed to compare the performance of three different approaches: 1. `array.push` (with conditionals) 2. `set.add` 3. `map.set` (with overwrite) These approaches are used to add elements to an array, set, or map, respectively. **Options Compared** * **Array push**: This approach uses the `push()` method of the array to add new elements. The twist is that some of these elements are added conditionally using if-else statements. * **Set.add**: This approach uses the `add()` method of the set data structure to add new elements. * **Map.set** (with overwrite): This approach uses the `set()` method of the map data structure to add new key-value pairs, with some keys being added multiple times. **Pros and Cons of Each Approach** * **Array push (with conditionals)**: + Pros: Simple and intuitive to implement. + Cons: Can be slower due to conditional checks, which may incur additional overhead. * **Set.add**: + Pros: Generally faster than array push due to its optimized implementation. + Cons: May not be suitable for use cases where duplicate values are allowed. * **Map.set** (with overwrite): + Pros: Allows for efficient storage and retrieval of key-value pairs, especially when keys are unique or have a specific order. + Cons: May be slower than set.add due to the additional overhead of storing and retrieving key-value pairs. **Library Usage** The benchmark uses native JavaScript data structures: * `Set`: An unordered collection of unique values. * `Map`: An unordered collection of key-value pairs. No external libraries are required for this benchmark. **Special JS Features or Syntax** None. The benchmark only uses standard JavaScript syntax and features. **Alternative Approaches** Other approaches that could be used to measure performance include: * Using a different data structure, such as an `ArraySet` (available in some browsers) or a custom implementation of a set or map. * Incorporating additional complexity, such as sorting or filtering the added elements. * Measuring performance under specific constraints, such as limited memory or concurrent execution. Keep in mind that these alternatives would likely introduce new variables and complexities, making it more challenging to accurately compare their performance with the current benchmark.
Related benchmarks:
set.add vs array.push vs map.set
Array from() vs Map.keys() vs Map.values() vs spread
set.add vs array.push Fabien
set.add vs array.push vs map.set
Comments
Confirm delete:
Do you really want to delete benchmark?