Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
endsWith vs includes test file names
(version: 1)
Comparing performance of:
.endsWith vs .includes
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = 'test.ts'; var stringToCheck = 'cfdsfdsffdsfs.test.ts'; var string2ToCheck = 'jfkdsljfdkslfjds.test.ts'; var result = null; var result2 = null;
Tests:
.endsWith
result = stringToCheck.endsWith(string); result2 = string2ToCheck.endsWith(string);
.includes
result = stringToCheck.includes(string); result2 = string2ToCheck.includes(string);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
.endsWith
.includes
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/133.0.0.0 Safari/537.36
Browser/OS:
Chrome 133 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.endsWith
1706731.4 Ops/sec
.includes
584810.1 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
### Overview of the Benchmark This benchmark compares two JavaScript string methods: `.endsWith` and `.includes`. The benchmark aims to measure the performance of these methods when checking if a given string (in this case, a file name) is included at the end of, or anywhere within, another string. ### Testing Options 1. **`.endsWith` Method:** - **Purpose:** This method checks if a string ends with the characters of a specified string. It returns `true` if the string ends with the specified substring, and `false` otherwise. - **Example in this context:** ```javascript result = stringToCheck.endsWith(string); ``` - **Performance:** - For the test case, this method had an execution speed of approximately **1,706,731.375 executions per second**. 2. **`.includes` Method:** - **Purpose:** This method determines whether one string can be found within another string, returning `true` if found, and `false` otherwise. - **Example in this context:** ```javascript result = stringToCheck.includes(string); ``` - **Performance:** - In this benchmark, this method had an execution speed of about **584,810.125 executions per second**. ### Pros and Cons #### 1. **.endsWith** - **Pros:** - Simple use case for checking suffixes. Efficient when you only need to verify how a string ends. - Directly optimized for end-of-string checks. - **Cons:** - Limited to checking the end of the string only. Cannot check for occurrences elsewhere. #### 2. **.includes** - **Pros:** - More general-purpose; it can check for substrings anywhere in the target string. - Good for scenarios where you want to verify if a string is part of another. - **Cons:** - Performs slower in this specific test case as established by the benchmark results, likely because it has to check multiple positions within the string. ### Performance Considerations The dramatically higher performance of the `.endsWith` method compared to the `.includes` method indicates that, for cases where the requirement is simply to check if a string ends with a specific substring, it's preferable to use `.endsWith`. Use `.includes` when a broader substring search is needed; however, developers should be aware that it might introduce overhead in performance-sensitive applications compared to `.endsWith`. ### Other Alternatives In JavaScript, other string methods or techniques can be used depending on the requirements: 1. **Regular Expressions:** - Regular expressions can be used to perform complex pattern matching and checks but may be overkill for simple suffix or substring checks. They can be less performant than these direct string methods. 2. **IndexOf Method:** - While not as modern or clear as `.includes`, `string.indexOf(substring) !== -1` can still be used to check for the presence of a substring. However, it does not indicate where the substring is located and can be less performant compared to modern methods. 3. **Substring Comparison:** - For specific cases, developers might extract a substring using `string.slice(-length)` and compare it directly. This can be more manual and may introduce additional complexity in code. ### Conclusion This benchmark effectively illustrates the performance differences between `.endsWith` and `.includes`, guiding developers in making informed choices regarding string checks in their JavaScript code. For suffix verification, prefer `.endsWith`, while for generalized substring checks, use `.includes`, weighing the performance impacts as needed.
Related benchmarks:
.endsWith vs includes
endsWith vs includes vs ===
.endsWith vs includes
String.includes vs String.match 3
.endsWith vs last char
.endsWith vs last char check
indexOf vs includes in strings
endsWith vs Includes
endsWith vs includes test file names 2
Comments
Confirm delete:
Do you really want to delete benchmark?