Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.endsWith vs includes
(version: 0)
Comparing performance of:
.endsWith vs .includes
Created:
6 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var string = 'car'; var stringToCheck = 'car'; 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:
3 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
14690290.0 Ops/sec
.includes
14316387.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is being tested?** The provided JSON represents a benchmark test case that compares the performance of two string methods: `.endsWith()` and `.includes()`. The benchmark tests how fast these two methods can be used to check if a given string ends with or includes another string. **Options compared:** There are only two options being compared: 1. `.endsWith(string)`: This method checks if the current string ends with the provided `string`. 2. `.includes(string)`: This method checks if the current string includes the provided `string`. **Pros and Cons of each approach:** * `.endsWith()`: This method is more efficient when checking for a specific suffix, as it can stop searching once it finds the desired character. However, it may be slower for longer strings or when searching for a prefix. * `.includes()`: This method scans the entire string to check if the provided `string` is present anywhere in the current string. It's generally slower than `.endsWith()` but has the advantage of being more inclusive (i.e., checking for any occurrence, not just the end). **Library and purpose:** There is no specific library mentioned in this benchmark test case. However, both methods are built-in JavaScript functions. **Special JS feature or syntax: None** There's nothing special about the code that would require any advanced JavaScript features or syntax beyond basic string manipulation. **Other alternatives:** If you need to optimize string comparisons, there might be alternative approaches: * Use a regular expression with the `g` flag to search for a pattern in a string. * Utilize `String.prototype.startsWith()` and `String.prototype.endsWith()` (in that order) to check if a string starts or ends with a specific value. Keep in mind that these alternatives might not directly compare to `.endsWith()` and `.includes()`, but they can provide different optimization paths for specific use cases. **Benchmark preparation code:** The provided script preparation code creates two variables: * `string`: A constant string variable set to `'car'`. * `stringToCheck`: Another constant string variable set to `'car'`. This setup is intentionally simple to focus on the performance differences between `.endsWith()` and `.includes()`. In real-world scenarios, you might want to use more complex strings or include additional variables to test different edge cases. Now that we've dissected the benchmark, it's clear that measuring performance can be a fascinating topic in software engineering. By comparing basic methods like `.endsWith()` and `.includes()`, we can gain insights into how these functions execute and optimize for real-world use cases.
Related benchmarks:
.endsWith vs includes
.endsWith vs includes betterment
endsWith vs Includes
.endsWith vs includes (2)
Comments
Confirm delete:
Do you really want to delete benchmark?