Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Array push / includes VS Set.add / has
(version: 0)
Comparing performance of:
Array vs Set
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var array = []; var set = new Set(); var events = ['reset','ready','change','default','destroy']; var toCheck = ['default', 'notthere', 'reset', 'detroit', 'change', 'nope'];
Tests:
Array
events.forEach(event => array.push(event)); let x = false; toCheck.forEach(event => { x = array.includes(event); });
Set
events.forEach(event => set.add(event)); let x = false; toCheck.forEach(event => { x = set.has(event); });
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array
Set
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 benchmarking framework and analyze what is being tested. **Benchmark Definition** The benchmark definition is represented by two JSON objects: `Benchmark Definition` and `Individual test cases`. * **Benchmark Definition**: This represents the overall benchmark, which includes a script preparation code, an HTML preparation code (empty in this case), and a description. The script preparation code creates an empty array (`array`) and a set (`set`). It also defines two arrays: `events` containing event names and `toCheck` containing test values. These arrays are used as input data for the benchmark. * **Individual Test Cases**: Each individual test case is represented by a JSON object, which includes: * **Benchmark Definition**: This represents a single test case within the overall benchmark. It contains JavaScript code that performs specific operations on the `array` and `set`. * **Test Name**: A descriptive name for each test case. Let's examine each individual test case in more detail: 1. **Array Test Case**: * The JavaScript code `events.forEach(event => array.push(event));` pushes all elements from the `events` array onto the `array`. * Then, it iterates over the `toCheck` array using `forEach`, and for each element, it checks if the element is present in the `array` using the `includes()` method. * The variable `x` is initialized to `false`. After the loop, its value will be determined by whether at least one of the elements from `toCheck` is found in the `array`. 2. **Set Test Case**: * Similar to the array test case, but this time it uses a set (`set`) instead. * The JavaScript code `events.forEach(event => set.add(event));` adds all elements from the `events` array onto the `set`. * Then, it iterates over the `toCheck` array using `forEach`, and for each element, it checks if the element is present in the `set` using the `has()` method. * The variable `x` remains unchanged in this test case. Now let's consider some pros and cons of these approaches: * **Array vs. Set**: * Pros: * Simpler implementation * Lower memory overhead for large datasets * Supports more operations (e.g., `indexOf()`, `lastIndexOf()`), which may be relevant in certain use cases. * Cons: * May be slower than using a set, especially when dealing with duplicate values or performing membership checks on large datasets. * **Push vs. Add**: * Pros: * Arrays are more widely supported and understood. * Less chance of errors due to type checking issues (e.g., adding non-numeric values in JavaScript). * Cons: * May be less efficient, especially for large datasets, because it requires shifting elements after each push operation. Other alternatives that could have been considered include: * Using a `Map` instead of an array or set. Maps provide key-value pairs and can support faster lookups compared to arrays or sets. * Implementing custom data structures based on the specific requirements of your use case, which might be more efficient but also more complex to implement. It's worth noting that these alternatives may not necessarily change the overall result of the benchmark, as they are generally used for different purposes. However, if performance is a critical aspect of your project, choosing the right data structure can significantly impact execution speed and efficiency.
Related benchmarks:
push vs unshift
push vs spread for single element addition to array
Add new element to array: push vs destructuring
set.add vs. array.push
set.add vs array.push Fabien2
Comments
Confirm delete:
Do you really want to delete benchmark?