Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.endsWith vs regex search
(version: 0)
Comparing performance of:
.endsWith vs regex
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var regex = '^.*\.(jpg|JPG|gif|GIF|doc|DOC|pdf|PDF)$'; var string = '.html' var stringToCheck = '/home/ashwin/code/webcrafters/drip-stream/drip-stream-fe/build/index.html'; var result = null;
Tests:
.endsWith
result = stringToCheck.endsWith(string);
regex
result = /^.*.(html)$/.test("/home/ashwin/code/webcrafters/drip-stream/drip-stream-fe/build/index.html");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.endsWith
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.endsWith
8213815.5 Ops/sec
regex
10178138.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the benchmark test. **What is being tested?** The benchmark tests two approaches to check if a string ends with another string: 1. `.endsWith()` method: This is a built-in JavaScript method that checks if a string ends with a specified value. 2. Regular expression search (`^.*.(html)$`): This uses a regular expression pattern to match the string against a specific format. **Options compared** The benchmark compares the performance of these two approaches on the same test case. **Pros and Cons** * `.endsWith()` method: + Pros: - Simple and concise implementation - Fast execution, as it only needs to check the last characters of the string + Cons: - May be slower for strings with a large number of characters, as it needs to concatenate the indices of the last matching character * Regular expression search (`^.*.(html)$`): + Pros: - Can be used to match more complex patterns than `.endsWith()` - Can be optimized using flags and anchoring for better performance + Cons: - More verbose implementation - May be slower due to the overhead of compiling and executing the regular expression **Library/ Framework** There is no specific library or framework used in this benchmark, only built-in JavaScript methods. **Special JS feature/Syntax** The `^.*.(html)$` syntax uses an anchor (`^`) at the start of the string, which ensures that the search starts from the beginning of the string. This is a common technique to prevent partial matches. **Other alternatives** Other approaches to check if a string ends with another string might include: * Using the `slice()` method to extract the last characters of the string and comparing them with the specified value * Using a custom implementation with a loop or recursion to iterate over the characters of the string However, `.endsWith()` is likely the most efficient and common approach in JavaScript. In terms of alternatives to the regular expression search, you might consider using more optimized approaches like: * Using a `lastIndexOf()` method to find the last index of the specified value in the string * Using a simple loop to iterate over the characters of the string and compare them with the specified value However, these alternatives may not be as concise or readable as the regular expression search.
Related benchmarks:
endsWith vs Regex extension
endsWith vs Regex correct
endsWith vs Regex-literal
RegEx.test vs. String.includes x 2
Comments
Confirm delete:
Do you really want to delete benchmark?