Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string includes vs startsWith vs startsWith with trimStart
(version: 1)
Comparing performance of:
includes vs startsWith vs trimStart + startsWith
Created:
9 months ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
/*your preparation JavaScript code goes here To execute async code during the script preparation, wrap it as function globalMeasureThatScriptPrepareFunction, example:*/ async function globalMeasureThatScriptPrepareFunction() { // This function is optional, feel free to remove it. // await someThing(); }
Tests:
includes
const str = `#EXTM3U #EXT-X-VERSION:6 #EXT-X-INDEPENDENT-SEGMENTS #EXT-X-STREAM-INF:BANDWIDTH=2250000,AVERAGE-BANDWIDTH=1628000,CODECS="avc1.64001e,mp4a.40.2",RESOLUTION=406x720,FRAME-RATE=30.000 audio-video/720/stream.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=1500000,AVERAGE-BANDWIDTH=1128000,CODECS="avc1.640015,mp4a.40.2",RESOLUTION=270x480,FRAME-RATE=30.000 audio-video/480/stream.m3u8`; str.includes("#EXTM3U")
startsWith
const str = `#EXTM3U #EXT-X-VERSION:6 #EXT-X-INDEPENDENT-SEGMENTS #EXT-X-STREAM-INF:BANDWIDTH=2250000,AVERAGE-BANDWIDTH=1628000,CODECS="avc1.64001e,mp4a.40.2",RESOLUTION=406x720,FRAME-RATE=30.000 audio-video/720/stream.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=1500000,AVERAGE-BANDWIDTH=1128000,CODECS="avc1.640015,mp4a.40.2",RESOLUTION=270x480,FRAME-RATE=30.000 audio-video/480/stream.m3u8`; str.startsWith("#EXTM3U")
trimStart + startsWith
const str = `#EXTM3U #EXT-X-VERSION:6 #EXT-X-INDEPENDENT-SEGMENTS #EXT-X-STREAM-INF:BANDWIDTH=2250000,AVERAGE-BANDWIDTH=1628000,CODECS="avc1.64001e,mp4a.40.2",RESOLUTION=406x720,FRAME-RATE=30.000 audio-video/720/stream.m3u8 #EXT-X-STREAM-INF:BANDWIDTH=1500000,AVERAGE-BANDWIDTH=1128000,CODECS="avc1.640015,mp4a.40.2",RESOLUTION=270x480,FRAME-RATE=30.000 audio-video/480/stream.m3u8`; str.trimStart().startsWith("#EXTM3U")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
includes
startsWith
trimStart + startsWith
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
includes
161857856.0 Ops/sec
startsWith
72691920.0 Ops/sec
trimStart + startsWith
51559004.0 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated 9 months ago):
This benchmark evaluates the performance of various string-searching methods in JavaScript, specifically comparing `includes`, `startsWith`, and a combination of `trimStart` followed by `startsWith`. The benchmark uses a specific string that appears to represent a playlist in the M3U8 format, commonly used for streaming media. ### Test Cases Explained 1. **`includes`**: ```javascript const str = `#EXTM3U\r\n#EXT-X-VERSION:6...`; // M3U8 playlist string str.includes("#EXTM3U"); ``` - **Purpose**: Checks if the string contains the substring `#EXTM3U`. - **Performance**: The test results show that this method has the highest performance in terms of "Executions Per Second" (161,857,856.0). - **Pros**: Simple and straightforward to use for checking the presence of a substring, allowing the search to occur anywhere within the string. - **Cons**: If searching for a specific starting position is required, additional logic is necessary, as `includes` does not achieve that. 2. **`startsWith`**: ```javascript const str = `#EXTM3U\r\n#EXT-X-VERSION:6...`; // M3U8 playlist string str.startsWith("#EXTM3U"); ``` - **Purpose**: Checks if the string starts with the substring `#EXTM3U`. - **Performance**: This method has a significantly lower performance (72,691,920.0 executions per second) compared to `includes`. - **Pros**: More efficient for checking if a string begins with a specific substring, which can be useful in specific scenarios where the starting position is known or relevant. - **Cons**: Limited to checking the beginning of the string only, which may not be sufficient for searches that require more flexibility. 3. **`trimStart().startsWith`**: ```javascript const str = `#EXTM3U\r\n#EXT-X-VERSION:6...`; // M3U8 playlist string str.trimStart().startsWith("#EXTM3U"); ``` - **Purpose**: First removes whitespace from the start of the string (if any exist) and then checks if the cleaned string starts with `#EXTM3U`. - **Performance**: This method is the slowest of the three (51,559,004.0 executions per second). - **Pros**: Useful if there's a possibility of leading whitespace affecting the check, ensuring that the check is accurate. - **Cons**: The combination of operations (trimming followed by checking) inherently makes it less efficient. Trimming adds additional overhead, which isn't necessary if you're confident about the structure of the input string. ### General Considerations - **String Length and Content**: The length and content of the string can significantly affect the performance of these operations. The benchmark string used is a reasonable size, allowing the operations to be meaningfully compared. - **Benchmarking Context**: The results may vary across different browsers and environments due to differences in JavaScript engines and their optimizations. - **Alternative Approaches**: Alternatives to these string methods could include using regular expressions, which can provide a more powerful search capability but at the cost of performance. However, regex is usually slower and less efficient for simple substring checks like the ones tested here. In conclusion, when choosing between these methods, the decision depends largely on the specific requirements of the task—whether you need a general substring check, a start-of-string check, or to handle potential leading whitespace. The performance trade-offs demonstrate the importance of choosing the right tool for the job.
Related benchmarks:
Assigning new variable
Multiplication vs Math.exp
Luxon vs
js mul vs pow
test vs exec
test slice czourhgirzjgprz
dadadada
byte double
アイウエオか聞くけど
Comments
Confirm delete:
Do you really want to delete benchmark?