Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes large213
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = [1, 2, 3, 4, 5, 6, 7, 8, , 2, 3, 4, 5, 6, 7, 8, , 2, 3, 4, 5, 6, 7, 8, , 2, 3, 4, 5, 6, 7, 8, , 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set(a)
Tests:
includes
return a.includes(9)
lookup
return b.has(9)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
lookup
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 explain what's being tested. **Benchmark Overview** The MeasureThat.net website allows users to create and run JavaScript microbenchmarks. The provided benchmark measures the performance difference between two approaches: using `array.includes()` versus using a `Set` data structure (`has()` method) for large arrays. **Script Preparation Code** The script preparation code creates an array `a` with 40 elements, including duplicates (e.g., multiple instances of numbers 2 to 10). Then, it creates a new `Set` object `b` from the array `a`. ```javascript var a = [1, 2, 3, 4, 5, 6, 7, 8, , 2, 3, 4, 5, 6, 7, 8, , 2, 3, 4, 5, 6, 7, 8, , 2, 3, 4, 5, 6, 7, 8, , 2, 3, 4, 5, 6, 7, 8, 9, 10]; var b = new Set(a); ``` **Test Cases** The benchmark consists of two test cases: 1. `includes` * Benchmark Definition: `return a.includes(9)` * Test Name: includes 2. `lookup` * Benchmark Definition: `return b.has(9)` * Test Name: lookup These test cases measure the performance of looking up an element (in this case, the number 9) in the array `a` using `array.includes()` versus using a `Set` object (`b`) and its `has()` method. **Performance Comparison** The benchmark measures the execution speed of each test case, which is reported as `ExecutionsPerSecond`. This value represents how many times the test function can execute per second. In the provided latest benchmark result: * The `includes` test case performs approximately 15897310.0 executions per second on Chrome 101 running on a Mac OS X 10.15.7 system. * The `lookup` test case performs approximately 14761513.0 executions per second on Chrome 101 running on the same system. **Pros and Cons** The choice between using `array.includes()` versus a `Set` object (`b`) depends on various factors: * **Performance**: Using a `Set` object can be faster for large arrays because it uses a hash table, which provides constant-time lookups. However, creating a `Set` object requires additional memory and processing time. * **Memory Usage**: Using an array with duplicates will require more memory than using a `Set` object, especially for very large datasets. * **Readability**: Using `array.includes()` is often more readable and easier to understand, as it clearly conveys the intention of looking up an element in the array. **Library: Set** The `Set` data structure is a built-in JavaScript object that provides an efficient way to store unique values. It's implemented as a hash table, which allows for fast lookups (average time complexity O(1)) and insertion/deletion operations. In this benchmark, using a `Set` object (`b`) provides a significant performance advantage over using `array.includes()` because of its constant-time lookups. **Special JS Feature/ Syntax: None** There are no special JavaScript features or syntax used in this benchmark. It's purely focused on measuring the performance difference between two basic JavaScript concepts: array lookup versus set-based lookup.
Related benchmarks:
convert to set + set.has vs. array.includes
set.has vs. array.includes bigger sample
set.has vs. array.includes (with big array)
set.has vs. array.includes (with big array and last index match)
set.has vs. array.includes (300 elements)
Comments
Confirm delete:
Do you really want to delete benchmark?