Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
substring vs indexOf
(version: 0)
Comparing performance of:
substring vs indexof
Created:
4 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var example = 'there is no spoon'
Tests:
substring
var result = example.substring(10)
indexof
var result = example.indexOf("there")
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
substring
indexof
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 days ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/150.0.0.0 Safari/537.36 Edg/150.0.0.0
Browser/OS:
Chrome 150 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
substring
761.0M/s
indexof
757.9M/s
View exact numbers
Test name
Executions per second
✓
substring
761,006,784 Ops/sec
indexof
757,873,664 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested, compared, and their pros/cons. **Benchmark Definition** The benchmark is comparing two different approaches: `substring` and `indexOf`. Both methods are used to extract a subset of characters from a string. The goal is to determine which method is faster. **Script Preparation Code** The script preparation code is the same for both tests: ```javascript var example = 'there is no spoon'; ``` This creates a sample string that will be used for the benchmark. **Html Preparation Code** There is no HTML preparation code provided, which means this is a simple JavaScript-only benchmark. **Individual Test Cases** The two test cases are: 1. **substring** ```javascript var result = example.substring(10); ``` This method extracts 9 characters starting from index 10 (not 11, since indexing starts at 0). 2. **indexOf** ```javascript var result = example.indexOf("there"); ``` This method searches for the substring "there" in the original string. **Library Used** Neither `substring` nor `indexOf` use any external libraries. They are built-in JavaScript methods. **Special JS Feature/Syntax** None of these methods require special JavaScript features or syntax beyond standard JavaScript syntax. **Comparison** The benchmark is comparing two approaches to extract a subset of characters from a string: 1. **Substring**: This method directly extracts a range of characters from the original string. 2. **IndexOf**: This method searches for a substring within the original string and returns its index (or -1 if not found). Pros and Cons: * **Substring**: + Pros: More straightforward, easier to implement, and potentially faster since it's a direct operation on the string data. + Cons: May be slower if the resulting slice is large or if the original string is very long. * **IndexOf**: + Pros: Can be faster for small substrings, as it only searches once. + Cons: May be slower overall due to the search operation. **Other Alternatives** If you wanted to compare these methods with alternative approaches: 1. Using `slice()` instead of `substring`: This would have similar performance characteristics to `substring`, but uses a different method to extract the slice. 2. Using regular expressions: This would be slower than both `substring` and `indexOf`, as it involves additional overhead for pattern matching. Keep in mind that this benchmark is focused on comparing two specific methods, rather than exploring other alternatives.
Related benchmarks
substr vs substring (with end index)
slice vs substring (with no end index)
Comments
Confirm delete:
Do you really want to delete benchmark?