Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. array.includes vs in operator (with strings)
(version: 0)
Comparing performance of:
includes vs lookup vs in
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var a = []; for (let i = 0; i < 100_000; i++) { a.push(i.toString()); } var b = new Set(a) var c = {} for (const key of a) { c[key] = 0; }
Tests:
includes
return a.includes("9")
lookup
return b.has("9")
in
return "9" in c
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
lookup
in
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 JSON data and explain what's being tested. **Benchmark Description** The benchmark measures the performance of three different approaches for checking if an element exists in a collection: 1. `array.includes()`: A method to check if a specified value exists in an array. 2. `set.has()`: A method to check if a value exists in a Set data structure. 3. The "in" operator: A keyword that checks if a value exists as a property of an object. **Script Preparation Code** The script preparation code creates three arrays: * `a`: An array with 100,000 elements, each being the string representation of a number from 0 to 99,999. * `b`: A Set created by pushing all elements of `a` into it. * `c`: An empty object that will be used as a test subject for the "in" operator. The script then creates an array `d` with the same elements as `a`, but in reverse order. This is not directly relevant to the benchmark, but might affect the results if it's not accounted for. **Html Preparation Code** There is no HTML preparation code provided. **Individual Test Cases** Each test case measures the performance of one of the three approaches: 1. `includes`: Measures the performance of `array.includes()`. 2. `lookup`: Measures the performance of `set.has()`. 3. `in`: Measures the performance of the "in" operator on object `c`. **Library and Purpose** * No libraries are explicitly mentioned in the provided code. * However, it's worth noting that the `Set` data structure is a built-in JavaScript API for working with collections. **Special JS Feature or Syntax** The benchmark uses arrays and objects as test subjects. The "in" operator checks if an element exists as a property of an object, which is a language feature in JavaScript. **Pros and Cons of Different Approaches** Here's a brief overview of the pros and cons of each approach: 1. `array.includes()`: * Pros: Fast, widely supported, and easy to use. * Cons: May perform poorly for very large arrays due to its reliance on searching through the array from start to end. 2. `set.has()`: * Pros: Very fast for checking existence in a set, especially when dealing with unique values. * Cons: May have higher overhead for inserting and deleting elements, which can impact overall performance. 3. "in" operator: * Pros: Fast and efficient for checking presence as an object property. * Cons: May be slower than the other two approaches for very large objects or arrays. **Other Alternatives** If you were to use a different approach or library for this benchmark, some alternatives could include: * Using `Map` instead of an object (`c`) for the "in" operator test case. * Utilizing libraries like Lodash or Ramda for additional functionality or performance enhancements. * Experimenting with other data structures like linked lists or trees. Keep in mind that these alternatives would require significant changes to the benchmark code and might not provide a fair comparison.
Related benchmarks:
set vs array iteration 100k elements
set vs array iteration many
set.has vs. array.includes vs in operator
set vs array iteration new new
Comments
Confirm delete:
Do you really want to delete benchmark?