Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.startsWith vs .indexOf
(version: 0)
fork of https://www.measurethat.net/Benchmarks/Show/975/11/regex-vs-indexof-vs-startswith-vs-substr
Comparing performance of:
startsWith vs indexOf start
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div></div>
Script Preparation code:
window.regexStart = /^test/; window.regexEnd = /test$/; window.match = 'test'; window.matchLength = window.match.length; var data = window.data = []; const possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; var TOTAL_STRINGS = window.TOTAL_STRINGS = 100000; function getRandomInt(max) { return Math.floor(Math.random() * max); } function makeRandomString(len) { var text = ""; for( var i=0; i < len; i++ ) { text += possible.charAt(getRandomInt(possible.length)); } return text; } while (data.length < TOTAL_STRINGS) { data.push(makeRandomString(getRandomInt(20))); }
Tests:
startsWith
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; var count = 0 while (x < TOTAL_STRINGS) { const str = data[x]; if(str.startsWith(match)) count++ x += 1; }
indexOf start
var x = 0; var TOTAL_STRINGS = window.TOTAL_STRINGS; var data = window.data; var match = window.match; var count = 0 while (x < TOTAL_STRINGS) { const str = data[x]; if(str.indexOf(match) === 0) count++; x += 1; }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
startsWith
indexOf start
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/147.0.0.0 Safari/537.36
Browser/OS:
Chrome 147 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
startsWith
723.4 Ops/sec
indexOf start
214.4 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided benchmark measures the performance of JavaScript code that uses two different string methods: `startsWith` and `indexOf`. Let's dive into what's being tested, compared, and the pros and cons of each approach. **What is being tested?** The test cases compare the execution time of two strings in a large dataset. The dataset consists of 100,000 randomly generated strings, each between 1 and 20 characters long. The test aims to determine which method is faster: `startsWith` or `indexOf`. **Options compared** There are two options being compared: ### Option 1: `startsWith` `startsWith` returns a boolean indicating whether the string starts with the specified value. ```javascript if(str.startsWith(match)) count++ ``` Pros: * Simple and concise syntax. * Fast execution, as it only needs to check if the string matches the prefix. Cons: * May not be suitable for larger datasets or longer prefixes, as it can lead to slower performance due to unnecessary comparisons. ### Option 2: `indexOf start` `indexOf` returns the index of the first occurrence of the specified value in the string. To make it comparable to `startsWith`, we're checking if the index is greater than 0: ```javascript if(str.indexOf(match) === 0) count++ ``` Pros: * More flexible, as it can handle prefixes and suffixes. * Can be faster for longer strings or larger datasets. Cons: * May have slower performance due to the need to scan the entire string for the match. **Other considerations** The benchmark uses a variety of factors to optimize performance, such as: * Using a large dataset with random strings. * Avoiding unnecessary computations by only iterating over the data when needed. * Using a simple and concise syntax for each test case. **Library or special JS feature** There are no libraries used in this benchmark. The code is self-contained and relies solely on built-in JavaScript features. **Special JS feature (none)** No special JavaScript features or syntax are used in this benchmark. Now, let's talk about other alternatives: In general, the choice between `startsWith` and `indexOf` depends on the specific use case and performance requirements. Here are some alternative approaches you might consider: * **Regular Expressions**: For more complex pattern matching, regular expressions can be a good option. * **Substrings**: If you need to search for substrings within a larger string, using `substr` or `substring` methods might be more suitable. * **Slicing and Comparison**: Instead of using `startsWith` or `indexOf`, you could use slicing and comparison operations to achieve the same result. Keep in mind that each approach has its own pros and cons, and the best choice depends on your specific requirements and performance constraints.
Related benchmarks:
JS Regex vs .startsWith vs .indexOf
JS Regex vs .startsWith vs .indexOf by Jarvis
JS Regex vs .startsWith vs .indexOf simpler
JavaScript: Regex vs .startsWith vs .indexOf
JS startsWith vs split upgraded
Comments
Confirm delete:
Do you really want to delete benchmark?