Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
endsWith() vs Ordinary For loop
(version: 0)
Comparing performance of:
endsWith() vs For loop
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'Hooman'; var ending = 'man';
Tests:
endsWith()
function solution(str, ending){ return str.endsWith(ending); }
For loop
function solution(str, ending){ for (let i = str.length -1, j = ending.length - 1; j >= 0; i--, j--) {if (str[i] !== ending[j]) return false} return true }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
endsWith()
For loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 123 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
endsWith()
100725432.0 Ops/sec
For loop
103108264.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided benchmark compares the performance of two approaches for checking if a string ends with a specific substring: the `endsWith()` method and an ordinary for loop. **Tested Options** The benchmark tests two options: 1. **`endsWith()`**: This is a built-in JavaScript method that checks if a string ends with another string. 2. **Ordinary For Loop**: This approach uses a traditional for loop to iterate through the characters of the original string and compare them with the last characters of the ending substring. **Pros and Cons** * `endsWith()` has several advantages: + It's a built-in method, so it's likely to be implemented efficiently by the JavaScript engine. + It's concise and easy to read. + It's a good choice when working with strings in most cases. * Ordinary For Loop has some drawbacks: + It requires manual memory management (loop variables) and can lead to performance issues if not optimized properly. + It's less readable than the `endsWith()` method, especially for complex string comparisons. However, there are also scenarios where the Ordinary For Loop might be preferred: * When working with very large strings or need more control over the iteration process. * In situations where the JavaScript engine doesn't support the `endsWith()` method (e.g., older browsers). **Other Considerations** When choosing between these two approaches, consider the following factors: * **String size**: For small to medium-sized strings, `endsWith()` is likely to be faster. For very large strings, the Ordinary For Loop might be more efficient. * **Readability**: If readability is important, use `endsWith()`. Otherwise, consider using the Ordinary For Loop for its flexibility. **Library and Special JS Features** There is no specific library used in this benchmark. However, if you're working with a custom library or framework that provides an optimized string comparison function, you might want to use it instead of the built-in methods. No special JavaScript features are mentioned in this benchmark. If you have any questions about other features or libraries, feel free to ask! **Alternatives** Other alternatives for string comparisons include: * **Regular Expressions**: Using regular expressions can provide more flexibility and control over the comparison process. * **String methods like `startsWith()`` or `includes()`: These methods might be faster in certain scenarios, depending on the specific use case. Keep in mind that the choice of string comparison method depends on your specific requirements and performance constraints.
Related benchmarks:
substr vs endsWith
String indexOf vs startsWith/endsWith
endswith vs indexof
String.includes vs. multiple String.endsWith
Comments
Confirm delete:
Do you really want to delete benchmark?