Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
indexOf vs split for finding sub string
(version: 0)
testing speed
Comparing performance of:
indexOf vs split
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var test1 = 'what a test tester testing' var test2 = 'test tester' var test3 = 'mytest'
Tests:
indexOf
var result1 = (test1.indexOf('test') == 1); var result2 = (test2.indexOf('test') == 1); var result3 = (test3.indexOf('test') == 1);
split
var result1 = (test1.split('test')[0].length == 1); var result2 = (test2.split('test')[0].length == 1); var result3 = (test3.split('test')[0].length == 1);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
indexOf
split
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!
Autogenerated LLM Summary
(model
llama3.1:latest
, generated one year ago):
Let's dive into the details of this benchmark. **What is being tested?** This benchmark compares two different approaches to find the occurrence of a substring within a string: 1. Using the `indexOf()` method 2. Using the `split()` method **Test Case 1: indexOf()** In the first test case, we're using the `indexOf()` method to check if the substring `'test'` exists in each of three strings (`test1`, `test2`, and `test3`). The test is checking whether the result of `indexOf()` equals 1 (which means the substring starts at index 0). ```javascript var result1 = (test1.indexOf('test') == 1); var result2 = (test2.indexOf('test') == 1); var result3 = (test3.indexOf('test') == 1); ``` **Test Case 2: split()** In the second test case, we're using the `split()` method to split each of the three strings (`test1`, `test2`, and `test3`) into an array of substrings whenever the delimiter `'test'` is encountered. The test then checks whether the length of the first element in the resulting array equals 1 (which means the substring starts at index 0). ```javascript var result1 = (test1.split('test')[0].length == 1); var result2 = (test2.split('test')[0].length == 1); var result3 = (test3.split('test')[0].length == 1); ``` **Library and JavaScript Features** In this benchmark, no external libraries are used. The test cases rely solely on built-in JavaScript methods (`indexOf()` and `split()`). **Pros and Cons of each Approach** 1. **indexOf()** * Pros: + Faster for searching substring occurrences + More efficient when dealing with large strings or multiple searches * Cons: + Returns `-1` if the substring is not found (not relevant in this test case) 2. **split()** * Pros: + Can be used to split a string into an array of substrings + Can be useful when dealing with complex string manipulation or parsing * Cons: + Slower than `indexOf()` for searching substring occurrences (as shown in the benchmark) + Creates additional overhead due to the creation of arrays and strings **Other Alternatives** There are other ways to search for a substring within a string, such as using regular expressions (`RegExp`) or iterating over the characters of the string. However, these approaches might be slower or more complex than `indexOf()` or `split()`, respectively. **Benchmark Results** The latest benchmark results show that: * Using `indexOf()` method is faster (9621658.0 executions per second) compared to using the `split()` method (6014002.5 executions per second). These results suggest that when searching for substring occurrences, using the `indexOf()` method might be a better choice than using the `split()` method, especially for large strings or multiple searches.
Related benchmarks:
split vs substring
string split vs string substring 2
slice vs substr vs substring (with no end index) 1
String.split vs String.substring
indexOf vs test
Comments
Confirm delete:
Do you really want to delete benchmark?