Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Split vs Substr+IndexOf Performance 2
(version: 0)
Comparing performance of:
Split vs Substring vs Substring 2
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))
Substring 2
var i = s1.indexOf('`') + 1; var resultSubstr = s1.substring(i, s1.indexOf('`', i))
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Split
Substring
Substring 2
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:121.0) Gecko/20100101 Firefox/121.0
Browser/OS:
Firefox 121 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Split
19078330.0 Ops/sec
Substring
26423322.0 Ops/sec
Substring 2
25712008.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. **Benchmark Definition** The benchmark is testing three different approaches to extract a substring from a string: `split`, `substring` with `indexOf`, and another variant of `substring` that uses two consecutive calls to `indexOf`. Here are the options being compared: 1. **`split"`**: This method splits the input string into an array of substrings using whitespace as the delimiter. In this case, it's used to extract a single substring from the original string. 2. **`substring` with `indexOf"`**: This method extracts a portion of the string that starts at a specified index and has a specified length. In this case, it's used to extract a substring starting at the index immediately after the backtick (`) character. 3. **Variant 1: Two consecutive calls to `indexOf`** (used in "Substring 2"): This method extracts a portion of the string that starts at a specified index and has a length of 1, using two consecutive calls to `indexOf` to find the start and end indices. **Pros and Cons** * **`split"`**: Pros: simple, efficient. Cons: may not be the most efficient way to extract a single substring, as it creates an array of substrings. * **`substring` with `indexOf"`**: Pros: flexible, easy to use. Cons: may have performance issues if used incorrectly (e.g., if the index is incorrect or if the string contains many consecutive occurrences of the delimiter). * **Variant 1: Two consecutive calls to `indexOf`**: Pros: similar to `substring` with `indexOf`, but avoids creating an array of substrings. Cons: slightly more complex, may have performance issues if used incorrectly. **Library Usage** None of the benchmarked code snippets use any external libraries. **Special JavaScript Features or Syntax** None of the benchmarked code snippets use any special JavaScript features or syntax (e.g., async/await, Promises). **Other Alternatives** If you wanted to write a similar benchmark, here are some alternative approaches you could consider: 1. **Using a regular expression**: You could use a regular expression to extract the desired substring from the input string. 2. **Using `replace` method**: You could use the `replace` method to replace all occurrences of a pattern with an empty string and then take the resulting substring. 3. **Using a custom implementation**: You could write your own custom function to extract substrings using a different algorithm or data structure. Keep in mind that these alternatives may have different performance characteristics than the benchmarked code snippets, so you would need to adjust the benchmark accordingly.
Related benchmarks:
slice vs substr vs substring (with end index) @fran
slice vs substr vs substring (with no end index) - 2
Split vs Substr+IndexOf Performance
slice vs substr (negative index)
Comments
Confirm delete:
Do you really want to delete benchmark?