Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
time complexity
(version: 0)
Comparing performance of:
set.has vs Array.includes
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
const s = "He is a very very good boy, isn't he?" const specialChar = ['!', '?', ',', '.', '_', '@']; const specialCharSet = new Set(specialChar);
Tests:
set.has
const setFunct = (s) => { let ArrOfChar = s.split(''); const ArrWithoutSpecialChar = []; for (let i = 0; i < ArrOfChar.length; i++) { if (!specialCharSet.has(ArrOfChar[i])) { if (ArrOfChar[i] === "'") ArrOfChar[i] = ' '; ArrWithoutSpecialChar.push(ArrOfChar[i]); } } let cleanArr = ArrWithoutSpecialChar.join('').split(' '); console.log(cleanArr.length); cleanArr.forEach((token) => console.log(token)); };
Array.includes
const arrFunct = (s) => { let ArrOfChar = s.split(''); const ArrWithoutSpecialChar = []; for (let i = 0; i < ArrOfChar.length; i++) { if (!specialChar.includes(ArrOfChar[i])) { if (ArrOfChar[i] === "'") ArrOfChar[i] = ' '; ArrWithoutSpecialChar.push(ArrOfChar[i]); } } let cleanArr = ArrWithoutSpecialChar.join('').split(' '); console.log(cleanArr.length); cleanArr.forEach((token) => console.log(token)); };
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set.has
Array.includes
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):
**What is being tested?** The provided benchmark tests two different approaches for removing special characters from a string: using the `Set` data structure (`set.has`) and using the `includes()` method on an array (`Array.includes`). The test cases are identical, except that one uses `includes()` and the other uses `has()`. **Options being compared** The two options being compared are: 1. **Using a Set (`set.has`)**: * Pros: Fast lookups, efficient data structure. * Cons: May require extra memory to store the set, can be slower for small inputs due to hashing overhead. 2. **Using `includes()` on an array (`Array.includes`)**: * Pros: Fast and efficient, doesn't require extra memory. * Cons: Can be slower than using a Set for large inputs, as it has to check every element in the array. **Other considerations** * The use of a `Set` is likely to be faster because looking up an element in a set typically involves a single hash operation, whereas checking if an element is present in an array often requires iterating through the entire array. * However, for small inputs, the overhead of creating and managing a Set may outweigh any potential benefits. * The use of `includes()` on an array can be slower than using a Set because it has to check every element in the array. **Library and its purpose** The `Set` data structure is a built-in JavaScript object that allows for efficient lookups, insertions, and deletions. In this benchmark, it's used to quickly check if an element is present in the `specialCharSet`. **Special JS feature or syntax** There are no special features or syntax being tested in this benchmark. **Benchmark preparation code explanation** The script preparation code defines two functions: * `setFunct`: Removes special characters from a string using a Set. * `arrFunct`: Removes special characters from a string using the `includes()` method on an array. Both functions take the same input (a string) and produce the same output (an array of clean tokens). The difference lies in how they approach removing special characters. **Other alternatives** Other approaches to removing special characters from a string could include: * Using a regular expression with the `g` flag, which would match all occurrences in the string. * Using the `replace()` method with a regex pattern, which would replace all matches with an empty string. * Using a library like Lodash's `remove` function, which provides a concise way to remove special characters from a string. However, these alternatives may not be as efficient or readable as using a Set or the `includes()` method on an array.
Related benchmarks:
cycle vs regex
cycle vs regex
WordsToNumbers2
replaceAll vs regex replace vs neu parser
Comments
Confirm delete:
Do you really want to delete benchmark?