Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
JS includes vs set
(version: 0)
Comparing performance of:
includes vs set
Created:
3 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr = Array.from({ length: 1000000 }, (_, i) => i); var set = new Set(arr);
Tests:
includes
arr.includes(987654);
set
set.has(987654)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
set
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36
Browser/OS:
Chrome 132 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
10499.2 Ops/sec
set
156911600.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the JavaScript microbenchmark on MeasureThat.net. **Benchmark Definition** The benchmark is designed to test the performance difference between using `includes()` and checking for existence in an array (or set) using the `has()` method. **Options Compared** Two options are being compared: 1. **`arr.includes(987654)`**: This option uses the built-in `includes()` method to check if a specific value (`987654`) exists in the array `arr`. 2. **`set.has(987654)`**: This option uses the `has()` method of a Set object, which is essentially an unordered collection of unique values. **Pros and Cons** Here's a brief analysis of each approach: ### `arr.includes(987654)` Pros: * Easy to read and write * Widely supported in modern browsers Cons: * Can be slower for large arrays because it has to iterate over the entire array to find the value. * May not be as efficient as using a Set or other data structure specifically designed for fast lookup. ### `set.has(987654)` Pros: * Extremely fast for large datasets, as sets use a hash table for lookup. * Memory-efficient, as sets only store unique values. Cons: * Requires creating a Set object and passing in the array to create it. * May be less readable than using `includes()` for some developers. **Library/Technology** The `Set` data structure is a built-in JavaScript library. It's an unordered collection of unique values that allows you to add, remove, and test existence of elements efficiently. **Special JS Feature/Syntax** There are no specific special features or syntax used in this benchmark other than the use of `includes()` and `has()`, which are standard methods in modern JavaScript. **Other Alternatives** If you need faster lookup performance than using a Set, you could consider alternative data structures like: * Arrays with binary search (for large datasets) * Hash tables (e.g., `Map` object in JavaScript) * Other data structures designed for fast lookup, such as Tries or suffix trees However, these alternatives might require additional code and are not as widely supported as using a Set. In summary, the benchmark tests the performance difference between using `includes()` and checking for existence in an array (or set) using the `has()` method. The `set.has(987654)` option is significantly faster for large datasets due to its use of a hash table, but requires creating a Set object and passing in the array.
Related benchmarks:
Create Set vs loop
Array.from vs. ... expansion
set vs some 1000000
set vs array iteration 100k elements
Comments
Confirm delete:
Do you really want to delete benchmark?