Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
object vs set.has vs. array.includes on strings test 4
(version: 3)
Comparing performance of:
includes vs lookup vs object
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
const randomString = () => (Math.random() + 1).toString(36).substring(2) var a = Array.from({ length: 500 }).map(() => randomString()) var b = new Set(a); var c = a.reduce((a, v) => ({ ...a, [v]: v }), {}) var str = a[Math.round(Math.random() * 500)]
Tests:
includes
return a.includes(str)
lookup
return b.has(str)
object
return c[str]
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
lookup
object
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 Definition** The benchmark is designed to compare three different approaches for checking if a string exists in an array: 1. `Array.prototype.includes(str)` 2. `Set.prototype.has(str)` 3. `Object.prototype[str]` (note that this is not directly applicable to arrays, but we'll assume it's meant to test the lookup functionality) **Script Preparation Code** The script generates 500 random strings and creates three data structures: 1. `a`: an array of the generated strings 2. `b`: a Set containing all elements of `a` 3. `c`: an object with string values that mirror the keys in `b` (i.e., each value is the same as its corresponding key) **Html Preparation Code** There is no HTML preparation code provided, so we'll assume it's not necessary for this benchmark. **Individual Test Cases** The test cases are: 1. `includes`: checks if a random string exists in `a` using the `includes()` method. 2. `lookup`: checks if a random string exists in `b` using the `has()` method of the Set object. 3. `object`: checks if a random string exists in `c` using the bracket notation (`[str]`) with the Object prototype. **Options Compared** The benchmark is comparing three different approaches: 1. `Array.prototype.includes(str)`: uses the `includes()` method on arrays 2. `Set.prototype.has(str)`: uses the `has()` method on Sets 3. `Object.prototype[str]`: uses bracket notation with the Object prototype (note that this is not directly applicable to arrays) **Pros and Cons** Here's a brief overview of each approach: 1. **Array.prototype.includes(str)**: * Pros: widely supported, easy to use * Cons: may be slower for large arrays due to the need to iterate over elements 2. **Set.prototype.has(str)**: * Pros: efficient and fast for lookups * Cons: requires a Set object, which may not be necessary in all scenarios 3. **Object.prototype[str]**: * Pros: can be used with objects that don't have properties with the same name as keys * Cons: less intuitive and more verbose than the other two options **Library Usage** The benchmark uses the following libraries: 1. `Array.from()`: creates an array from a iterable object (in this case, a numeric value) 2. `Set()`: creates a Set object from an array of elements 3. `Object.prototype` and bracket notation (`[]`): used to create an object with string values that mirror the keys in the Set **Special JS Feature/Syntax** None of the test cases use any special JavaScript features or syntax, apart from the fact that they're using ES6+ syntax (e.g., arrow functions, template literals). **Alternatives** If you need to compare these approaches for arrays instead of Sets, you could modify the `b` variable to be an array instead of a Set. The `includes()` method would then become the only option being compared. Alternatively, if you want to test the performance of these methods on objects instead of arrays or Sets, you could create an object with string properties and use bracket notation (`[str]`) to access them.
Related benchmarks:
Set.has vs. Array.includes
set.has vs. array.includes Performance 2
set.has vs. array.includes Perf Random
set.has vs. array.includes Perf Random 1
Comments
Confirm delete:
Do you really want to delete benchmark?