Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Split vs Substr+IndexOf Performance
(version: 0)
Comparing performance of:
Split vs Substring
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var s1 = "28.9% CPU samples were in `Array.Sort` called from `DiagService.Controllers.PerfCPUController.CpuTask`. We expect this value to be less than 0.7%."
Tests:
Split
var resultSplit = s1.split[1];
Substring
var resultSubstr = s1.substring(s1.indexOf('`') + 1, s1.indexOf('`', s1.indexOf('`') + 1))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Split
Substring
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:148.0) Gecko/20100101 Firefox/148.0
Browser/OS:
Firefox 148 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Split
37031792.0 Ops/sec
Substring
43452724.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what's being tested in this JavaScript microbenchmark. **Benchmark Overview** The benchmark compares the performance of two approaches to extract a substring from a given string: `split` and `substring + indexOf`. The test aims to determine which approach is faster. **Options Compared** Two options are compared: 1. **`split`**: This method splits a string into an array of substrings using a specified separator (in this case, no separator). It then returns the second element of the resulting array. 2. **`substring + indexOf`**: This approach uses the `indexOf` method to find the index of the first occurrence of a specific substring (`' '`), and then uses the `substring` method to extract the substring from the original string. **Pros and Cons** 1. **`split`**: * Pros: Simple, efficient, and widely supported. * Cons: May not be suitable for strings without a separator, and it can return an array with additional elements if the separator is present multiple times. 2. **`substring + indexOf`**: * Pros: Can handle strings without a specific substring to find, and it returns only the extracted substring. * Cons: Requires two method calls ( `indexOf` followed by `substring` ), which can lead to overhead. **Library/Functionality Used** None of the test cases use any libraries or external functions. The functionality is built-in to JavaScript. **Special JS Features/Syntax** There are no special JavaScript features or syntax used in this benchmark. **Other Alternatives** Other approaches to extract a substring from a string could be: * Using regular expressions (e.g., `s1.match(/\s/g)[0]`) * Using the `slice` method (e.g., `s1.slice(s1.indexOf(' ') + 1, s1.indexOf(' ') + 2)`) * Using a custom loop to iterate through the string and find the substring However, these alternatives are not being tested in this specific benchmark. **Benchmark Preparation Code** The provided "Script Preparation Code" is used to prepare the input string `s1` before running the benchmarks. It's used to extract the CPU usage percentage from a string, which seems unrelated to the actual benchmarking task. I hope this explanation helps you understand what's being tested in this JavaScript microbenchmark!
Related benchmarks:
split vs substring
slice vs substr vs substring (with no end index) - 2
Split vs Substr+IndexOf Performance 2
slice vs substr (negative index)
Comments
Confirm delete:
Do you really want to delete benchmark?