Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string split vs indexOf vs yield 2
(version: 0)
Comparing performance of:
split vs indexOf vs yield
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = "123 ".repeat(1000000)
Tests:
split
var split = str.split(" ") var count = 0; for (const s of split) count++;
indexOf
var count = 0; var i = 0; while (i >= 0) { count++; i = str.indexOf(" ", i + 1); }
yield
function* splitIter(str, needle) { let i = 0; while (i >= 0) { const iOld = i; i = str.indexOf(needle, i + 1); if (i > 0) yield str.substring(iOld, i) else if (iOld < str.length) yield str.substring(iOld) } } let count = 0 for (const s of splitIter(str, " ")) count++;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
split
indexOf
yield
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
split
12.2 Ops/sec
indexOf
16.9 Ops/sec
yield
26.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a platform that allows users to create and run JavaScript microbenchmarks, measuring the performance of various algorithms and data structures on different browsers and devices. The provided benchmark definition and test cases are used to compare the performance of three string splitting methods: 1. **Split Method**: uses the `split()` function to split the input string into an array of substrings. 2. **IndexOf Method**: uses the `indexOf()` function to find the index of the first occurrence of a specified delimiter in the input string, and then iterates over the resulting indices to count the number of delimiters found. 3. **Yield Method**: uses a generator function (`splitIter`) that yields substrings from the input string by finding consecutive occurrences of a specified delimiter. **Approach Comparison** Each approach has its pros and cons: * **Split Method** * Pros: * Easy to implement * Fast for most use cases * Cons: * May not be suitable for very large input strings due to memory constraints * Can be slower than `indexOf` or `yield` methods for specific edge cases * **IndexOf Method** * Pros: * Suitable for large input strings, as it iterates over indices rather than creating an array of substrings * Fast for most use cases * Cons: * Can be slower than the `split` method due to repeated calls to `indexOf()` * May have performance issues with very long input strings or specific edge cases * **Yield Method** * Pros: * Suitable for large input strings and can handle repeated delimiter occurrences efficiently * Fast for most use cases, especially when dealing with long input strings * Cons: * More complex implementation than the other two methods * May require more overhead due to the use of a generator function **Library Considerations** The `splitIter` function in the `Yield Method` uses the `indexOf()` function under the hood, which is a part of the JavaScript standard library. This means that the benchmark results will be influenced by the browser's implementation of `indexOf()`. **Special JS Feature or Syntax** There are no special JavaScript features or syntax used in this benchmark except for generator functions (`yield`), which are a standard feature in modern JavaScript. **Other Alternatives** If you were to implement this benchmark again, some alternative approaches could be explored: * **Regular Expressions**: using `RegExp.split()` or `RegExp.exec()` to split the input string could provide a more flexible and efficient solution. * **Array.prototype.map()**: using `map()` on an array created by `split()` or another method could provide an alternative implementation of the `split` method. * **Custom loop**: implementing a custom loop using a different iteration strategy, such as a `while` loop with manual index management, could potentially outperform the existing implementations. It's worth noting that each approach has its trade-offs and may be more or less suitable depending on the specific use case and performance requirements.
Related benchmarks:
Performance Test: substring vs substr vs slice constant length
Performance Test: indexOf + slice vs split
String.split vs String.substring
IndexOf Array vs String
string split vs indexOf iteration
Comments
Confirm delete:
Do you really want to delete benchmark?