Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes string values
(version: 0)
Comparing performance of:
includes vs lookup
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j"]; var b = new Set(a)
Tests:
includes
return a.includes("i")
lookup
return b.has("i")
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:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:124.0) Gecko/20100101 Firefox/124.0
Browser/OS:
Firefox 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
36048392.0 Ops/sec
lookup
1498898688.0 Ops/sec
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 Purpose** The primary goal of this benchmark is to compare the performance of two approaches: using `Array.includes()` for searching a string value in an array, versus using a `Set` data structure (specifically, the `has()` method) to achieve the same result. **Options Compared** Two options are being compared: 1. **`Array.includes()`**: This method searches for the specified value (`"i"` in this case) in the array and returns a boolean indicating whether it's present. 2. **`Set.has()`**: In this approach, a `Set` is created from an array of strings, and then the `has()` method is used to check if a specific string value (`"i"`) is present in the set. **Pros and Cons** * **`Array.includes()`**: + Pros: Simple, widely supported, and easy to understand. + Cons: Can be slower than using a data structure like `Set`, especially for large arrays or sets. * **`Set.has()`**: + Pros: Generally faster than `Array.includes()`, as it uses a hash table data structure for lookups. + Cons: May require additional setup (creating the `Set`), and not all browsers support this method. **Library/Utility Used** In this benchmark, no specific library is used beyond what's built into JavaScript. However, using a `Set` does rely on the browser's implementation of set data structures, which may vary between engines (e.g., V8 in Chrome vs. SpiderMonkey in Firefox). **JavaScript Features/Syntax** This benchmark doesn't use any advanced JavaScript features or syntax that would affect its understanding. **Benchmark Preparation Code** The preparation code creates two variables: * `a`: an array of strings containing values `"a"` to `"j"`. * `b`: a new `Set` created from the elements of `a`. **Individual Test Cases** Each test case consists of a single benchmark definition, which is executed to measure performance. The two test cases are: 1. **`includes`**: Searches for the string value `"i"` in array `a` using `Array.includes()`. 2. **`lookup`**: Searches for the string value `"i"` in set `b` using its `has()` method. **Latest Benchmark Result** The latest results show that the execution time for each test case is different: * **`lookup` (using `Set.has()`)**: 36048392 executions per second. * **`includes` (using `Array.includes()`)**: 1498898688 executions per second. This suggests that using a data structure like `Set` can provide significant performance improvements for certain types of lookups.
Related benchmarks:
convert to set + set.has vs. array.includes
Includes (array) vs Has (Set)
set.has vs. array.includes (string input)
Array includes vs Set.has
set.has (w/ creation) vs. array.includes
Comments
Confirm delete:
Do you really want to delete benchmark?