Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
substr vs startsWith
(version: 0)
Benchmark.js
test perf of substr vs startsWith
Comparing performance of:
substr vs startsWith
Created:
8 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var str = "abcdefghijklmnopqrstuvwxyz";
Tests:
substr
return str.substr(0,10) == 'abcdefghij'
startsWith
return str.startsWith('abcdefghij')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
substr
startsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
7 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:147.0) Gecko/20100101 Firefox/147.0
Browser/OS:
Firefox 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
startsWith
590.4M/s
substr
579.0M/s
View exact numbers
Test name
Executions per second
✓
startsWith
590,398,400 Ops/sec
substr
578,967,616 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested, compared options, pros and cons, library usage, special JS features, and other considerations. **Benchmark Definition** The benchmark is defined in two test cases: 1. `substr`: Tests the performance of using the `substr` method to extract a substring from the string `str`. 2. `startsWith`: Tests the performance of using the `startsWith` method to check if the string starts with a specified substring. **Options Compared** In both test cases, the options being compared are: * Using `substr` vs. using `startsWith`. **Pros and Cons of Each Approach** 1. **Using `substr`:** * Pros: + More flexible, as it allows you to extract any part of the string. + Can be used for more complex substring matching tasks. * Cons: + May require more overhead due to the need to calculate the start index. 2. **Using `startsWith`:** + Pros: + Generally faster and more lightweight, as it only checks if a prefix exists. + More efficient in terms of execution speed. + Cons: + Less flexible than `substr`, as it can only check for exact prefixes. **Library Usage** There is no explicit library usage mentioned in the JSON. However, JavaScript's built-in string methods, including `substr` and `startsWith`, are used. **Special JS Features or Syntax** There are no special JavaScript features or syntax used in this benchmark that would require explanation. **Other Considerations** When choosing between `substr` and `startsWith`, consider the specific requirements of your use case. If you need to extract a specific substring, `substr` might be a better choice. However, if you only need to check for an exact prefix, `startsWith` is likely faster and more efficient. **Alternatives** Other alternatives to `substr` and `startsWith` include: * Using a regular expression to achieve similar results. * Implementing custom substring or prefix checking logic using bitwise operators or other techniques. * Using a library like Lodash, which provides additional string manipulation methods. Keep in mind that the choice of method depends on your specific use case and performance requirements.
Related benchmarks
indexOf vs substr vs startsWith
Comments
Confirm delete:
Do you really want to delete benchmark?