Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Set.has v.s Array.includes v2
(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(maxLength) { const randomArray = []; for (let i = 0; i < maxLength; i++) { const randomInt = i; randomArray.push(i); } return randomArray; } function createTargetArray(maxLength, maxValue) { const randomArray = []; for (let i = 0; i < maxLength-1; i++) { const randomInt = i; randomArray.push(10001+i); } randomArray.push(maxValue); return randomArray; } var ids1 = createRandomIntArray(100); var ids2 = createRandomIntArray(1000); var ids3 = createRandomIntArray(10000); var target1 = createTargetArray(100, 99); var target2 = createTargetArray(1000, 999); var target3 = createTargetArray(10000, 9999);
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:
Run details:
(Test run date:
6 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
set
1162.9 Ops/sec
array
104.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in this benchmark. **Benchmark Definition** The benchmark is comparing two methods to check if an element exists in an array or a Set: 1. `Set.has()` method 2. `Array.includes()` method (specifically, the new version of `includes()` introduced in ECMAScript 2020, which allows passing a callback function). **Script Preparation Code** The script preparation code generates three arrays with random integers and one array with a target value. The arrays are created using two functions: * `createRandomIntArray(maxLength)`: creates an array with `maxLength` elements, where each element is a random integer between 0 and `maxLength - 1`. * `createTargetArray(maxLength, maxValue)`: creates an array with `maxLength` elements, where the last element is set to `maxValue`. **Benchmark Definition Json** The benchmark definition json contains two test cases: 1. "set": This test case uses the `Set.has()` method to check if each element in the arrays exists in the Set. 2. "array": This test case uses the new version of `Array.includes()` to check if each element in the arrays exists. **Options Compared** The benchmark is comparing two options: 1. Using `Set.has()` to check for existence 2. Using the new version of `Array.includes()` with a callback function **Pros and Cons** **Using `Set.has()`:** * Pros: + Faster lookup times compared to `Array.includes()` + More memory-efficient, as Sets only store unique values * Cons: + May not be suitable for large datasets or arrays with many duplicate values + May require additional setup or modifications to work correctly **Using the new version of `Array.includes()` with a callback function:** * Pros: + More flexible and adaptable than `Set.has()`, as it can handle more complex conditions + May be easier to understand and maintain, especially for developers familiar with functional programming concepts * Cons: + Slower lookup times compared to `Set.has()` + May consume more memory, especially if the callback function is complex or recursive **Library Used** The benchmark uses the JavaScript `Set` data structure, which is a collection of unique values. The Set data structure provides an efficient way to check for existence using the `has()` method. **Special JS Feature/Syntax** There are no special JavaScript features or syntax used in this benchmark, aside from the new version of `Array.includes()` introduced in ECMAScript 2020. **Other Alternatives** If you're interested in exploring alternative methods for checking existence in arrays or Sets, here are a few options: * Using `Object.prototype.hasOwnProperty.call()`: This method can be used to check if an element exists in an array or object, but it may not be the most efficient or readable solution. * Using a library like Lodash: This library provides a variety of utility functions for working with arrays and objects, including methods for checking existence. Keep in mind that the choice of method will depend on your specific use case and performance requirements.
Related benchmarks:
Labels
Set.has v.s Array.includes
yoooooo
Transform dict values: for vs fromEntries 2
Comments
Confirm delete:
Do you really want to delete benchmark?