Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
startsWith vs indexOf vs lastIndexOf
(version: 2)
Compare substring matching performance.
Comparing performance of:
startsWith positive vs indexOf positive vs lastIndexOf positive vs startsWith negative vs indexOf negative vs lastIndexOf negative vs substring positive vs substring negative
Created:
4 years ago
by:
Registered User
Jump to the latest result
Script Preparation code:
hay = 'https://url.com/Lorem%20ipsum%20dolor%20sit%20amet,%20consectetur%20adipiscing%20elit.%20Aenean%20quis%20rhoncus%20odio.%20In%20nec%20congue%20tellus.%20Nunc%20pulvinar,%20urna%20et%20pretium%20malesuada,%20nisi%20massa%20imperdiet%20sem,%20vitae%20euismod%20nunc%20felis%20vestibulum%20quam.%20Suspendisse%20potenti.%20Quisque%20ut%20massa%20risus.%20Integer%20ac%20nisl%20vehicula%20quam%20tincidunt%20porttitor.%20Ut%20lacinia%20urna%20odio,%20eget%20tincidunt%20sem%20eleifend%20vel.%20Maecenas%20tincidunt,%20urna%20in%20sagittis%20ultrices,%20turpis%20nisi%20varius%20lectus,%20scelerisque%20egestas%20nibh%20velit%20sit%20amet%20diam.%20Pellentesque%20ac%20tellus%20tellus.%20Maecenas%20scelerisque%20maximus%20elit,%20non%20congue%20tortor%20elementum%20at.' needle1 = 'https://url.com/Lorem%20ipsum%20dolor%20sit%20amet,%20consectetur%20adipiscing%20elit.%20Aenean%20quis%20rhoncus%20odio.%20In%20nec%20congue%20tellus.%20Nunc%20pulvinar,%20urna%20et%20pretium%20malesuada,%20nisi%20massa%20imperdiet%20sem,%20vitae%20euismod%20nunc%20felis%20vestibulum%20quam.%20Suspendisse%20potenti.%20Quisque%20ut%20massa%20risus.' needle2 = 'https://url.com/Ut%20tincidunt%20pretium%20diam,%20quis%20tincidunt%20lacus%20elementum%20nec.%20Etiam%20sit%20amet%20hendrerit%20augue,%20sollicitudin%20blandit%20nunc.%20Quisque%20finibus%20tortor%20ante,%20sit%20amet%20mollis%20neque%20tempor%20ac.%20Integer%20vestibulum%20ultricies%20nibh,%20sed%20condimentum%20augue%20hendrerit%20vitae.' console.assert(hay.startsWith(needle1)) console.assert(hay.indexOf(needle1) === 0) console.assert(hay.lastIndexOf(needle1, 0) === 0) console.assert(!hay.startsWith(needle2)) console.assert(!(hay.indexOf(needle2) === 0)) console.assert(!(hay.lastIndexOf(needle2, 0) === 0)) console.assert(hay.substring(0, needle1.length) === needle1) console.assert(!(hay.substring(0, needle2.length) === needle2))
Tests:
startsWith positive
hay.startsWith(needle1)
indexOf positive
hay.indexOf(needle1) === 0
lastIndexOf positive
hay.lastIndexOf(needle1, 0) === 0
startsWith negative
hay.startsWith(needle2)
indexOf negative
hay.indexOf(needle2) === 0
lastIndexOf negative
hay.lastIndexOf(needle1, 0) === 0
substring positive
hay.substring(0, needle1.length) === needle1
substring negative
hay.substring(0, needle2.length) === needle2
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (8)
Previous results
Fork
Test case name
Result
startsWith positive
indexOf positive
lastIndexOf positive
startsWith negative
indexOf negative
lastIndexOf negative
substring positive
substring negative
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!
Related benchmarks:
slice vs substr vs substring (with end index & large string)
slice vs substr vs substring (with no end index) and long text
String Test indexOf vs Search
IndexOf vs Includes in string - larger string edition
Comments
Confirm delete:
Do you really want to delete benchmark?