Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.endsWith vs includes (2)
(version: 0)
Comparing performance of:
.endsWith vs .includes
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = 'car'; var stringToCheck = 'abcdefghijkcar'; 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:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
.endsWith
9519974.0 Ops/sec
.includes
6688238.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested, compared, and other considerations. **Benchmark Definition** The benchmark is comparing two methods in JavaScript: `string.endsWith()` and `string.includes()`. These methods are used to check if a string ends or contains another string, respectively. **Script Preparation Code** The script preparation code creates two strings: `string` (which only has the characters "car") and `stringToCheck` (which is a longer string that contains "car" at the end). The purpose of this setup is to create a scenario where one method can correctly detect the presence or absence of the substring. **Comparison** The comparison involves running two iterations of each benchmark: 1. `.endsWith()`: This method checks if `stringToCheck` ends with `string`. 2. `.includes()`: This method checks if `stringToCheck` contains `string`. The results of these comparisons are measured in terms of the number of executions per second. **Pros and Cons** * **.endsWith()**: Pros: + More efficient for checking if a string ends with another substring, especially when dealing with large strings. + Less overhang (i.e., fewer false positives) compared to `.includes()` since it only checks for the exact match at the end of the string. * Cons: + May be less suitable for cases where you need to check for an arbitrary substring within the larger string, not just at the end. * **.includes()**: Pros: + More versatile for checking if a string contains any substring, regardless of its position. + May be more intuitive for developers who are used to using this method in other contexts (e.g., array methods). * Cons: + Less efficient for checking if a string ends with another substring, especially for large strings. + More likely to produce false positives due to the overhang (i.e., matching any part of the larger string). **Library and Special Features** In this benchmark, there is no explicit library being used. However, it's worth noting that both `.endsWith()` and `.includes()` are built-in methods in JavaScript, so they don't require any external dependencies. There are also no special features or syntax being tested in this benchmark. The focus is solely on comparing the performance of these two methods. **Alternatives** Other alternatives for checking if a string ends with another substring or contains an arbitrary substring include: * Using regular expressions (e.g., `string.match(/.*car$/)` or `string.match(/.*car/)`). * Implementing custom string methods using loop constructs (e.g., `function endsWith(str, suffix) { while (str.length >= suffix.length) { if (str.slice(-suffix.length) === suffix) return true; str = str.slice(0, -suffix.length); } return false; }`). * Using a third-party library that provides optimized string matching algorithms (e.g., [js-regex](https://github.com/michaelfreeman/js-regex)). Keep in mind that these alternatives may have different performance characteristics or trade-offs compared to using the built-in `.endsWith()` and `.includes()` methods.
Related benchmarks:
.endsWith vs includes
.endsWith vs includes
.endsWith vs includes betterment
endsWith vs Includes
Comments
Confirm delete:
Do you really want to delete benchmark?