Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set.has vs. string.includes
(version: 0)
Comparing performance of: string includes vs sets lookup
Comparing performance of:
includes vs has
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var alpha = 'abcdefghijklmnopqrstuvwxyz' var a = "Five quacking Zephyrs jolt my wax bed."; var b = new Set(a); var count = 0;
Tests:
includes
for (let char in alpha){ count += (a.includes(char) || a.includes(char.toUpperCase()) )?1:0; } console.log(count)
has
for (let char in alpha){ count += (b.has(char) || b.has(char.toUpperCase()) )? 1 : 0; } console.log(count)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
includes
has
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 benchmarking test cases and explain what is being tested, compared, and their pros and cons. **Benchmark Test Case 1: "string includes vs sets lookup"** The first test case compares the performance of two approaches: 1. **String `includes` method**: This approach uses the built-in string `includes` method to search for characters in the string `a`. 2. **Set lookup**: This approach uses a Set data structure (`b`) to store unique characters from the string `a`. It then checks if each character is present in the set using the `has` method. **Comparison** The test case measures the performance of both approaches by iterating through each character in the string `alpha`, and for each character, it increments a counter if the character is found in either the string `a` (case-insensitive) or the set `b` (also case-insensitive). **Pros and Cons:** * **String `includes` method**: + Pros: Simple to implement, widely supported. + Cons: May be slower for large strings due to substring search. * **Set lookup**: + Pros: Can be faster for large strings since it only checks for presence in a set, not searching through the entire string. + Cons: Requires additional memory allocation for the Set data structure and may have higher overhead. **Library Used:** None. The test case uses native JavaScript methods and data structures. **Special JS Feature or Syntax:** None mentioned. The alternative approaches to this benchmark would be: * Using a regular expression instead of the `includes` method. * Using a more efficient algorithm, such as Boyer-Moore string searching. * Using a library like `lodash` that provides optimized string matching functions. * Using a different data structure, such as an array or object, to store unique characters. **Benchmark Test Case 2: "for (let char in alpha){ count += (a.includes(char) || a.includes(char.toUpperCase()) )?1:0; }\r\nconsole.log(count)"** This test case is identical to the first one, but with a different variable name (`char`) and the `includes` method called on string `b`.
Related benchmarks:
set.has vs. array.includes (string input)
equality vs includes
set.has vs. array.includes (find 999,999 in 1,000,000)
String equals vs String.includes
equals vs includes (one value)
Comments
Confirm delete:
Do you really want to delete benchmark?