Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Test two string array if it has a matching lowercased string
(version: 0)
Comparing performance of:
Array.prototype.includes() vs RegExp.prototype.test()
Created:
2 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var arr1 = [ "HOPES", "AND", "DREAMS", "WERE", "DASHED", "THAT", "DAY.", "IT", "SHOULD", "HAVE", "BEEN", "EXPECTED,", "BUT", "IT", "STILL", "CAME", "AS", "A", "SHOCK.", "THE", "WARNING", "SIGNS", "HAD", "BEEN", "IGNORED", "IN", "FAVOR", "OF", "THE", "POSSIBILITY,", "HOWEVER", "REMOTE,", "THAT", "IT", "COULD", "ACTUALLY", "HAPPEN.", "THAT", "POSSIBILITY", "HAD", "GROWN", "FROM", "HOPE", "TO", "AN", "UNDENIABLE", "BELIEF", "IT", "MUST", "BE", "DESTINY.", "THAT", "WAS", "UNTIL", "IT", "WASN'T", "AND", "THE", "HOPES", "AND", "DREAMS", "CAME", "CRASHING", "DOWN." ] var arr2 = [ "THERE", "ARE", "DIFFERENT", "TYPES", "OF", "SECRETS.", "SHE", "HAD", "HELD", "ONTO", "PLENTY", "OF", "THEM", "DURING", "HER", "LIFE,", "BUT", "THIS", "ONE", "WAS", "DIFFERENT.", "SHE", "FOUND", "HERSELF", "HOLDING", "ONTO", "THE", "WORST", "TYPE.", "IT", "WAS", "THE", "TYPE", "OF", "SECRET", "THAT", "COULD", "GNAW", "AWAY", "AT", "YOUR", "INSIDES", "IF", "YOU", "DIDN'T", "TELL", "SOMEONE", "ABOUT", "IT,", "BUT", "IT", "COULD", "END", "UP", "GETTING", "YOU", "KILLED", "IF", "YOU", "DID." ]
Tests:
Array.prototype.includes()
const lowerCasedArr1 = arr1.map(str => str.toLowerCase()); arr2.some(str => lowerCasedArr1.includes(str.toLowerCase()));
RegExp.prototype.test()
const arr1Regex = new RegExp(arr1.join("|"),'i') arr2.some(str => arr1Regex.test(str))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Array.prototype.includes()
RegExp.prototype.test()
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) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
Browser/OS:
Chrome 119 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Array.prototype.includes()
672350.5 Ops/sec
RegExp.prototype.test()
288152.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its options. **Benchmark Definition** The benchmark is testing two different approaches to check if an array contains a specific string, after converting that string to lowercase. **Options Compared** 1. **Array.prototype.includes()**: This method checks if a given value (in this case, a string) exists in an array after converting it to lowercase. 2. **RegExp.prototype.test()**: This method uses a regular expression pattern to match the input string against an array of strings. The pattern is created by joining all elements of the array with the `|` character, which creates an "or" condition, and setting the `i` flag for case-insensitive matching. **Pros and Cons** * **Array.prototype.includes()**: + Pros: Simple to understand, widely supported across browsers. + Cons: May perform slower for large arrays or strings, as it needs to create a new array of lowercase values. * **RegExp.prototype.test()**: + Pros: Can be faster for large arrays or strings, as it only needs to create a single regular expression pattern. + Cons: More complex to understand, and may have performance issues if not used carefully. **Library Used** In the benchmark definition, `map()` is used from the Array prototype. It transforms each element of an array into a new value by applying a provided function. In this case, it's converting all strings in `arr1` to lowercase using the `toLowerCase()` method. **Special JS Feature/Syntax** None mentioned in the benchmark definition. **Other Alternatives** * **Array.prototype.indexOf()**: Instead of using `includes()`, you could use `indexOf()` which returns the index of the first occurrence of the specified value, or -1 if it's not found. This might be faster for large arrays. * **String.match()**: If you need to match a string against an array of strings, you could use `String.match()` instead of regular expressions. However, this method is not as flexible as `RegExp.prototype.test()`. * **Using a library like Lodash or Underscore.js**: These libraries provide utility functions for working with arrays and strings, which might be useful in certain scenarios. It's worth noting that the benchmark results show different execution rates for each test case. This suggests that the choice of approach can have significant performance implications, especially for large inputs or in sensitive use cases.
Related benchmarks:
string comparison vs array includes
indexOf vs includes vs some - 20211114
array indexOf vs includes vs some long list
Array some vs find
Array concat vs spread vs push spread, loop, apply (bigger cases)
Comments
Confirm delete:
Do you really want to delete benchmark?