Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.endsWith vs includes (longer string)
(version: 1)
Comparing performance of:
.endsWith vs .includes
Created:
one year ago
by:
Registered User
Jump to the latest result
Script Preparation code:
var string = 'healthz'; var stringToCheck = 'ThisIsAPrettyLongStringWhichFinishesWithACommonlyUsedRouteToCheckHealth:healthz'; var result = null;
Tests:
.endsWith
result = stringToCheck.endsWith(string);
.includes
result = stringToCheck.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:
4 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.endsWith
13111808.0 Ops/sec
.includes
13732921.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark test defined is comparing two JavaScript string methods: `.endsWith()` and `.includes()`, using a specific long string and a substring. Below is an explanation of what is tested, the options compared, pros and cons of each approach, relevant libraries, and alternatives. ### Benchmark Details 1. **Tested Methods:** - **`.endsWith()`**: This method checks if a given string ends with a specified substring. - **`.includes()`**: This method checks if a given string contains a specified substring at any position. 2. **Options Compared:** - The benchmark compares the performance of these two string methods when used to examine the string `stringToCheck`, which is defined as `'ThisIsAPrettyLongStringWhichFinishesWithACommonlyUsedRouteToCheckHealth:healthz'`, looking for the substring defined in `string`, which is `'healthz'`. ### Performance Results: - The benchmark results indicate that `.endsWith()` executes about **16,785,418 times per second**, while `.includes()` executes approximately **10,191,831 times per second**. This suggests that the `.endsWith()` method performs significantly better than the `.includes()` method in this specific case. ### Pros and Cons #### `.endsWith()` - **Pros**: - More efficient for checking if a string literally ends with a specific substring, particularly for longer strings where only the end needs to be validated. - Generally faster execution time as shown in the benchmark results. - **Cons**: - Limited to checking only the end of strings; cannot check for a substring within the entirety of the string. #### `.includes()` - **Pros**: - More versatile in that it checks for the existence of a substring anywhere within the string. - Useful for use cases where the position of the substring is irrelevant and only existence is needed. - **Cons**: - Slower performance compared to `.endsWith()` in this benchmark, reflecting its broader use case. - May lead to performance issues in scenarios where many checks are required on very long strings. ### Other Considerations - **Usage Context**: The choice between these methods depends on the specific requirements. If needing to verify string endings (like file extensions or specific patterns), `.endsWith()` is more suitable. If just the presence of a substring is needed irrespective of its position, `.includes()` is appropriate. - **Browser Compatibility**: Both methods are widely supported across modern JavaScript engines and browsers, making them safe for use in production environments. ### Alternatives - **Regular Expressions**: For more complex string pattern matching, you might consider using regular expressions with `RegExp.test()` for pattern matching. This can check for substrings in various positions or formats but may come with additional overhead. - **Indexing Methods**: Using the `indexOf()` method can also be an alternative for checking substring presence. It returns the index of the first occurrence of a specified value or -1 if not found. However, it is less intuitive than `.includes()` for the existence check. Choosing the right method depends on performance requirements and the specific string manipulation needs of the application. In performance-sensitive applications, it’s crucial to benchmark the scenarios that are most relevant to the use case.
Related benchmarks:
.endsWith vs includes
endsWith vs includes vs ===
.endsWith vs includes
.endsWith vs includes betterment
.endsWith vs last char
.endsWith vs includes longer
.endsWith vs last c
endsWith vs Includes
.endsWith vs includes (2)
Comments
Confirm delete:
Do you really want to delete benchmark?