Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
Feature Requests
FAQ
Register
Log In
confronto indexof substr
(version: 0)
Benchmark.js
Comparing performance of:
uno vs due
Created:
2 years ago
by:
Guest
Go to the latest result
Script Preparation code:
var x = 'pippo';
Tests:
uno
return x.indexOf('pi') > -1
due
return x.substr(0,2) == 'pi'
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
uno
due
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36
Browser/OS:
Chrome 122 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
uno
16.1M/s
due
14.3M/s
View exact numbers
Test name
Executions per second
✓
uno
16,132,229 Ops/sec
due
14,344,778 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the benchmark and its options. **What is tested?** The benchmark tests two different approaches for searching a substring within a string: `indexOf` and `substr`. **Options compared** There are two main options being compared: 1. **`indexOf()`**: This method returns the index of the first occurrence of the specified value in the string. If the value is not found, it returns -1. 2. **`substr()`**: This method extracts a section of characters from the original string. In this case, `substr(0, 2)` extracts the first two characters. **Pros and Cons** * `indexOf()`: Pros: + More straightforward to use for simple substring searches + Less CPU-intensive compared to `substr` Cons: + May not be as efficient for large strings or complex searches * `substr()`: Pros: + Can be more efficient than `indexOf()` for large strings or complex searches, as it doesn't require iterating over the entire string Cons: + More verbose and error-prone to use correctly **Library usage** There is no library used in this benchmark. Both `indexOf()` and `substr()` are built-in JavaScript methods. **Special JS feature/syntax** None mentioned. **Other alternatives** If you wanted to compare other approaches, some alternatives could be: * Using regular expressions (`RegExp`) * Using a library like Lodash or Underscore.js * Implementing a custom search algorithm Keep in mind that these alternatives would likely have different trade-offs and performance characteristics compared to the built-in `indexOf()` and `substr()` methods. **Benchmark preparation code** The provided script preparation code is simply assigning a string value `x` with the content `"pippo"`. The HTML preparation code is empty, which suggests that this benchmark doesn't require any specific HTML structure or rendering.
Related benchmarks
String.indexOf vs String.indexOf with the second parameter
String.indexOf(char) vs String.indexOf(char, position)
Comments
Confirm delete:
Do you really want to delete benchmark?