Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes Perf Random 1
(version: 0)
set.has vs. array.includes Perf Random 1
Comparing performance of:
Includes vs lookup
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = Array.from({length: 100}, () => Math.floor(Math.random() * 40)); var b = new Set(a);
Tests:
Includes
return a.includes(98)
lookup
return b.has(98)
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):
I'll break down the explanation into smaller parts to make it easier to understand. **Benchmark Definition and Script Preparation Code** The provided JSON represents a JavaScript benchmark that tests the performance of two different approaches: `array.includes` and `set.has`. The script preparation code initializes two arrays: * `a`: An array with 100 elements, where each element is a random number between 0 and 39 (inclusive). * `b`: A Set created from array `a`. The purpose of creating these two data structures is to provide a common baseline for the benchmark. The set `b` serves as a container for unique values extracted from array `a`, which will be used in the subsequent test cases. **Test Cases** There are two individual test cases: 1. **Includes**: This test case uses the `array.includes` method to search for a specific value (98) within array `a`. The goal is to measure the performance of this method. 2. **lookup**: This test case uses the `set.has` method to check if a specific value (98) exists in set `b`. Again, the objective is to evaluate the performance of this approach. **Library and Special JS Features** In both test cases: * A library called `Array.from()` is used to create an array with 100 random numbers. This function is a modern JavaScript method for creating arrays from iterables. * The use of `Math.floor(Math.random() * 40)` generates random numbers between 0 and 39 (inclusive). This is a common way to generate random values in JavaScript. **Pros and Cons** Here's a brief analysis of the two approaches: * **array.includes**: * Pros: + Widely supported across browsers. + Relatively fast due to its optimized implementation. * Cons: + Can be slower for large arrays or searching for specific values near the end of the array. + May perform poorly if the array contains duplicate values (since it has to linearly search through the array). * **set.has**: + Pros: - Fast and efficient, especially for unique values. - Can be faster than `array.includes` when searching for specific values near the beginning of the set. * Cons: - Not as widely supported across browsers (it's a newer feature). - Requires the array or set to be created first. **Other Alternatives** Some alternative approaches could have been used: * For `array.includes`, you could use the `indexOf()` method, which returns the index of the specified value. If the value is not found, it returns `-1`. * For `set.has`, a more traditional approach would be to use the `hasOwnProperty()` method or the `in` operator with an object. Keep in mind that these alternatives might have slightly different performance characteristics compared to the original methods used in the benchmark.
Related benchmarks:
Unique
Set.has vs. Array.includes
Array push vs
set.has vs. array.includes Perf Random
Comments
Confirm delete:
Do you really want to delete benchmark?