Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
endsWith vs includes test file names 2
(version: 1)
Comparing performance of:
.endsWith vs .includes
Created:
one year ago
by:
Guest
Go to the latest result
Script Preparation code:
var string = 'test.ts'; var stringToCheck = 'cfdsfdsffdsfs.test.ts'; 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:
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
.endsWith
12.3M/s
.includes
9.3M/s
View exact numbers
Test name
Executions per second
✓
.endsWith
12,297,714 Ops/sec
.includes
9,345,396 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The benchmark you provided tests two string methods in JavaScript: `.endsWith()` and `.includes()`, focusing specifically on their performance when applied to a string representing a filename. ### Options Compared 1. **.endsWith()**: - **Purpose**: This method checks whether a given string ends with the characters specified in another string. In the benchmark, it checks if `stringToCheck` (i.e., `'cfdsfdsffdsfs.test.ts'`) ends with the filename string (`'test.ts'`). - **Performance**: Based on the benchmark results, `.endsWith()` executed at approximately 12.3 million operations per second. 2. **.includes()**: - **Purpose**: This method determines whether a string contains a specified substring. In this case, it checks if `stringToCheck` contains the substring `string` (i.e., `'test.ts'`). - **Performance**: The benchmark results show that `.includes()` executed at around 9.3 million operations per second. ### Pros and Cons #### .endsWith() - **Pros**: - More efficient for its specific purpose of checking the end of a string, leading to higher execution speed. - Simple and straightforward for validation of string endings, making it an ideal choice for filename checks. - **Cons**: - Limited functionality: It only checks the end of the string and cannot be used for checking the presence of substrings elsewhere in the string. #### .includes() - **Pros**: - More versatile than `.endsWith()`, as it can check for the presence of the substring anywhere within the string. - Can be used in scenarios beyond filenames, such as content searching within larger strings. - **Cons**: - Slower performance in this specific benchmark scenario, potentially due to its more general nature of searching through the entire string rather than just examining the end. ### Alternative Approaches 1. **.substring() or .slice()**: - Instead of using `.endsWith()`, one could manually slice the string and compare it with the specified substring. This could add complexity and likely not perform as well as the native method. 2. **Regular Expressions**: - Using regular expressions (e.g., `/test\.ts$/`) can achieve similar results to both methods. However, regex can introduce additional overhead and complexity compared to the simpler string methods. 3. **Performance Considerations**: - The choice between these methods would depend on the use case. If only checking for the end of a string, `.endsWith()` is preferred for performance. If searching for a substring is needed, `.includes()` is the better choice, despite being slower in this benchmark. ### Conclusion The choice of string methods in JavaScript can significantly affect performance, as demonstrated in this benchmark between `.endsWith()` and `.includes()`. While both methods have their unique strengths and weaknesses, selecting the appropriate method based on the specific use case can lead to improved efficiency in code execution.
Related benchmarks
.endsWith vs includes
endsWith vs includes vs ===
.endsWith vs includes
.endsWith vs includes betterment
endsWith vs Includes
.endsWith vs includes (2)
endsWith vs includes test file names
Comments
Confirm delete:
Do you really want to delete benchmark?