Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.add test
(version: 0)
Comparing performance of:
set vs arr
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
set
const set = new Set() let check = 0 for (let i = 0; i < 10000; i++) { const val = Math.random() set.add(val) set.has(val) }
arr
const arr = [] let check = 0 for (let i = 0; i < 10000; i++) { const val = Math.random() arr.push(val) if (i > 10) check++ arr.includes(arr[i]) }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set
arr
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 world of JavaScript microbenchmarks! **Benchmark Overview** The provided JSON represents a set of benchmarking tests for measuring the performance of JavaScript code on various browsers and devices. The benchmarks are designed to test specific aspects of JavaScript, such as array operations, hash table operations (via `Set`), and conditional statements. **Test Cases** There are two individual test cases: 1. **set.add test**: This test creates a new `Set` object and adds 10,000 random values to it using the `add()` method. The test then checks if each added value is present in the set using the `has()` method. 2. **arr**: This test creates an empty array and pushes 10,000 random values into it. It then increments a counter (`check`) for every value pushed, but only after the first 10 iterations of the loop. The test checks if each pushed value is present in the array using the `includes()` method. **Options Compared** The two test cases compare different approaches to performing operations on data structures: * **Set.add**: This approach uses a hash table (specifically, a JavaScript `Set` object) to store unique values. It's a more efficient way to check for membership in a set, especially when dealing with large datasets. * **arr.includes**: This approach uses an array to store values and checks for presence using the `includes()` method. While arrays are convenient data structures, this approach is less efficient than using a hash table like `Set` for large datasets. **Pros and Cons** * **set.add**: + Pros: Efficient membership testing (O(1) on average), suitable for large datasets. + Cons: Requires creating a new set object, which can incur overhead. * **arr.includes**: + Pros: Simple to implement, no additional memory allocation required. + Cons: Inefficient for large datasets (O(n)), as it requires iterating through the entire array. **Libraries and Special JS Features** There is no specific library mentioned in this benchmark. However, it's worth noting that both `Set` and `includes()` methods are built-in JavaScript features, making them easily accessible and efficient. No special JavaScript features or syntax are used in these test cases, so they should be widely supported across modern browsers and environments. **Other Alternatives** If you wanted to optimize this benchmark, you could consider: * Using a more specialized data structure, such as a `Map` or a custom implementation of a hash table. * Utilizing browser-specific optimizations, like WebAssembly (WASM) or V8's Just-In-Time (JIT) compilation. * Exploring alternative JavaScript engines, like SpiderMonkey (Firefox), V8 (Chrome), or JavaScriptCore (Safari). * Considering other approaches to membership testing, such as using a binary search algorithm. Keep in mind that the best approach will depend on the specific requirements and constraints of your use case.
Related benchmarks:
new Set([x]).has vs [x].includes
New set vs set clear 2
array.includes vs. set.has on the fly
new Set vs set.clear()
incl/set
Comments
Confirm delete:
Do you really want to delete benchmark?