Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set vs checked array add
(version: 0)
Comparing performance of:
Set vs Checked array
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var data = [...new Array(100)].map((_, i) => i); var checkedPush = (array, item) => { for (let i = 0, len = array.length; i < len; ++i) { let isPresent = false; for (const oldItem of array) { if (oldItem === item) { isPresent = true; break; } } if (!isPresent) { array.push(item); } } }
Tests:
Set
var smallSet = new Set(); for (const item of data) { smallSet.add(item); }
Checked array
var smallArray = []; for (const item of data) { checkedPush(data, item); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Set
Checked array
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 provided benchmark and explain what's being tested. **Benchmark Overview** The benchmark compares two approaches for adding elements to an array: using a `Set` data structure (Option 1) versus checking if an element already exists in the array before adding it (Option 2). The goal is to determine which approach is faster. **Test Cases** There are two individual test cases: 1. **Set**: This test case uses a `Set` data structure to add all elements from the `data` array to a new set. A `Set` in JavaScript is an unordered collection of unique values. 2. **Checked Array**: This test case uses the custom `checkedPush` function to check if each element already exists in the `data` array before adding it to the array. **Options Compared** The benchmark compares two options: 1. **Option 1: Set Data Structure** * Pros: + Faster addition of unique elements (average time is around 100-150 ns) * Cons: + May not be suitable for cases where duplicate values are allowed or needed 2. **Option 2: Checked Array** * Pros: + Suitable for cases where duplicate values are allowed or needed * Cons: + Slower than using a Set data structure (average time is around 200-250 ns) **Other Considerations** When choosing between these two options, consider the following factors: * **Uniqueness**: If you need to ensure unique elements, using a `Set` data structure might be a better choice. * **Duplicate Values**: If allowing duplicate values or ensuring they are added even if already present is necessary, using the checked array approach might be more suitable. **Library Used** In the benchmark code, the `checkedPush` function uses a library called Lodash (not explicitly mentioned in the provided JSON). The `checkedPush` function is a custom implementation that checks for duplicates before adding an element to the array. Lodash is a popular JavaScript utility library providing functional programming helpers and iterables. **Special JS Feature or Syntax** There are no special JavaScript features or syntaxes used in this benchmark. However, it's worth noting that using `Set` data structures and custom functions like `checkedPush` might require some familiarity with modern JavaScript features, such as first-class functions and iterable objects. Now, let's look at the latest benchmark results: The results show that using a `Set` data structure (Option 1) is significantly faster than using the checked array approach (Option 2). The Safari 15 browser on Mac OS X 10.15.7 achieved an average execution time of around 100-150 ns for the Set test case, while the checked array test case took around 200-250 ns. As for alternatives, some other approaches to adding elements to an array include: * Using `Array.prototype.includes()` and `push()`: This method would require checking if each element already exists in the array before adding it, which is similar to Option 2. * Using a custom implementation with a hash table data structure: This approach would be similar to using a Set data structure but might have additional overhead due to its complexity. These alternatives are not mentioned in the provided benchmark code and would require further exploration for comparison.
Related benchmarks:
for vs some - searching for element
set.add vs array.push vs map.set
set.add vs array.push Fabien
array push vs reduce
Comments
Confirm delete:
Do you really want to delete benchmark?