Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
string split vs indexOf iteration
(version: 0)
Comparing performance of:
split vs indexOf walk
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 walk
var count = 0; var i = 0; while (i >= 0) { count++; i = str.indexOf(" ", i + 1); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
split
indexOf walk
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
9 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36 Edg/138.0.0.0
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
split
22.0 Ops/sec
indexOf walk
130.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Definition** The benchmark is defined by a JSON object that contains: * `Name`: a unique name for the benchmark * `Description`: an optional description of the benchmark (in this case, empty) * `Script Preparation Code`: a JavaScript code snippet that prepares the input data for the benchmark * `Html Preparation Code`: an optional HTML code snippet that can be used to prepare the environment for the benchmark In this case, both `Script Preparation Code` and `Html Preparation Code` are empty, which means that no additional setup is required. **Test Cases** The benchmark consists of two individual test cases: 1. **"split"`** * `Benchmark Definition`: a JavaScript code snippet that uses the `str.split()` method to split a string into an array of substrings. * Purpose: To measure the performance of the `str.split()` method. 2. **"indexOf walk"`** * `Benchmark Definition`: a JavaScript code snippet that uses a while loop with `str.indexOf()` to iterate over a string and count the number of occurrences of a specific character (`" "`, in this case). * Purpose: To measure the performance of the `str.indexOf()` method. **Library** In both test cases, the `split()` method is used from the built-in JavaScript Array prototype. No external libraries are required for these tests. **Special JS Features/Syntax** There are no special JavaScript features or syntax that are specific to this benchmark. Now, let's discuss the pros and cons of each approach: **Split Method (`str.split()`)** Pros: * Simple and efficient way to split a string into an array. * Built-in method with good performance characteristics. Cons: * Can be slower than manual iteration for very large strings. * Creates an intermediate array that may not be desirable in certain scenarios (e.g., memory-intensive applications). **IndexOf Method (`str.indexOf()`)** Pros: * More efficient than split method for small to medium-sized strings. * Allows for early exit if the character is not found. Cons: * Can be slower than manual iteration for very large strings. * May have performance issues with certain characters or Unicode ranges. **Other Alternatives** If you were to rewrite this benchmark, you could consider using alternative approaches, such as: 1. **Regex**: Instead of `str.split()`, you could use a regex replacement approach to achieve the same result. This would likely be slower than `str.split()` but might offer more flexibility for certain use cases. 2. **Manual Iteration**: You could implement manual iteration over the string using a loop, similar to the "indexOf walk" test case. This would allow you to control every aspect of the iteration and optimize for specific scenarios. In summary, both `str.split()` and `str.indexOf()` have their strengths and weaknesses, and the choice between them depends on the specific requirements of your application and the characteristics of your input data.
Related benchmarks:
Performance Test: substring vs substr vs slice constant length
Performance Test: indexOf + slice vs split
IndexOf Array vs String
Performance Test: substring vs substr vs slice vs split for date
Comments
Confirm delete:
Do you really want to delete benchmark?