Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
yoooooo
(version: 0)
Comparing performance of:
set vs array
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function getRandomInt(min, max) { return Math.floor(Math.random() * (max - min + 1)) + min; } function createRandomIntArray(minLength, maxLength) { const length = getRandomInt(minLength, maxLength); const randomArray = []; for (let i = 0; i < length; i++) { const randomInt = i; randomArray.push(getRandomInt(0, maxLength)); } return randomArray; } const minLength = 1000; const maxLength = 1001; var ids1 = createRandomIntArray(minLength, maxLength); var ids2 = createRandomIntArray(minLength, maxLength); var ids3 = createRandomIntArray(minLength, maxLength); var target1 = createRandomIntArray(1, 50); var target2 = createRandomIntArray(1, 50); var target3 = createRandomIntArray(1, 50);
Tests:
set
var idsSet1 = new Set(ids1) target1.some(id => idsSet1.has(id)) var idsSet2 = new Set(ids2) target2.some(id => idsSet2.has(id)) var idsSet3 = new Set(ids3) target3.some(id => idsSet3.has(id))
array
target1.some(id => ids1.includes(id)) target2.some(id => ids2.includes(id)) target3.some(id => ids3.includes(id))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set
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 its options. **Benchmark Definition** The benchmark is testing two different approaches to check if an element exists in an array: 1. Using `some()` method with a callback function that checks for existence in a `Set` object created from the original array (`set` option). 2. Using `includes()` method on the original array itself (`array` option). **Options Comparison** Here's a comparison of the two options, including their pros and cons: ### Set Option (Test Name: "set") * **Purpose**: To use a `Set` object to quickly check if an element exists in the array. * **Pros**: + Fast lookup time since `Set` operations are O(1) on average. + Can be more memory-efficient than using arrays for large datasets. * **Cons**: + Requires extra memory allocation for creating a new `Set` object. + May incur additional overhead due to the creation of the `Set` object. ### Array Option (Test Name: "array") * **Purpose**: To use the `includes()` method on the original array itself to check if an element exists. * **Pros**: + No extra memory allocation required. + Faster iteration over the array for small datasets. * **Cons**: + Slower lookup time since array searching is O(n) in the worst case. **Library and Purpose** The `Set` object is a built-in JavaScript library that provides a fast and efficient way to store unique values. In this benchmark, it's used to quickly check if an element exists in the original array by creating a new `Set` object from the array and then checking for existence using the `some()` method. **Special JS Feature or Syntax** In this benchmark, we're using the following JavaScript features: * Arrow functions (e.g., `id => idsSet1.has(id)`). * Template literals (e.g., `"var idsSet1 = new Set(ids1)\r\n..."`). These features are widely supported in modern browsers and are a part of the ECMAScript 2015 standard. **Alternatives** If you were to use alternative approaches for this benchmark, some options could be: * Using a `Map` object instead of `Set` to store the original array. * Implementing a custom lookup data structure (e.g., hash table) for faster lookups. * Using a third-party library like Lodash or Ramda for functional programming and optimization. However, these alternatives may not provide significant performance benefits over the current implementation and may add additional complexity.
Related benchmarks:
Labels
Fisher-Yates Shuffle
Set.has v.s Array.includes
Set.has v.s Array.includes v2
Comments
Confirm delete:
Do you really want to delete benchmark?