Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
substring vs endsWith
(version: 0)
Test substring vs endsWith where you have to calculate the length of the string for substring operation.
Comparing performance of:
substring vs endsWith
Created:
5 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var stringToCheck = '/asdasd/asdasd/asasd/asad/asdasd.html'; var stringToCheckLength = stringToCheck.length; var stringToFind = '.html'; var stringToFindLength = 5; var isStringFound = false;
Tests:
substring
if (stringToCheck.substring(stringToCheckLength - stringToFindLength) === stringToFind) { isStringFound = true; }
endsWith
if (stringToCheck.endsWith(stringToFind)) { isStringFound = true; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
substring
endsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested. **Benchmark Overview** The benchmark compares the performance of two string manipulation methods: `substring` and `endsWith`. The tests aim to find the substring in the input string `stringToCheck` that matches a predefined length (`stringToFindLength`) and checks if it exists using both methods. **Library Usage** There is no explicit library usage mentioned in the JSON. However, since the benchmark involves string manipulation, we can assume that the JavaScript engine being tested provides built-in support for these operations. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. The tests only involve standard JavaScript strings and methods. **Benchmark Preparation Code** The preparation code sets up two variables: * `stringToCheck`: a sample string containing the substring to search. * `stringToCheckLength`: the length of `stringToCheck`. * `stringToFind`: the predefined substring length (`'.html'`). * `stringToFindLength`: the actual length of `stringToFind` (5 characters). The preparation code initializes a variable `isStringFound` to keep track of whether the string is found using either method. **Test Cases** There are two test cases: 1. **substring**: This test case checks if the substring in `stringToCheck` that matches `stringToFindLength` exists by calling `substring()` with the correct offset. 2. **endsWith**: This test case checks if the last part of `stringToCheck` (i.e., its length minus the length of `stringToFind`) matches `stringToFind` using the `endsWith()` method. **Options Compared** The benchmark compares the performance of two methods: * `substring()`: calculates the offset for the substring by subtracting the desired length (`stringToFindLength`) from the total string length (`stringToCheckLength`). This approach is more explicit but may be slower due to the arithmetic operation. * `endsWith()`: uses a built-in method that checks if the last character of the string matches the expected value. This approach might be faster because it avoids manual calculation. **Pros and Cons** **substring():** Pros: * More explicit, making the code easier to understand for some developers Cons: * May be slower due to arithmetic operation * Requires manual calculation of offset **endsWith():** Pros: * Typically faster due to built-in optimization * Avoids manual calculation Cons: * Less explicit, which might make it harder for some developers to understand **Other Considerations** * The benchmark measures the number of executions per second (`ExecutionsPerSecond`) on a specific browser and device configuration. * The test cases assume that the input string `stringToCheck` is not null or empty. **Alternative Approaches** If you wanted to add alternative approaches, you could consider testing: 1. Using regular expressions (regex) instead of manual calculation. 2. Using a library like Lodash's `endsWith()` method for comparison. 3. Comparing performance with other string manipulation methods, such as `indexOf()` or `startsWith()`. 4. Testing the impact of different JavaScript engine optimizations on performance. These alternatives would require additional code and benchmark definitions to accommodate the changes.
Related benchmarks:
slice vs substring from end
Performance Test: substring vs substring over limit
Performance Test: substring vs substr vs slice 1
.endsWith vs last c
Comments
Confirm delete:
Do you really want to delete benchmark?