Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
substring vs substring+
(version: 0)
Comparing performance of:
substring + extra obj vs substring simple
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
var IDS = ["report_1", "report_1", "report_1", "report_1","report_1","report_1","report_1","report_1","report_1", "report_dsdsdssds_1", "report_dsdsdsds_1", "report_1", "report_1","report_1","report_1","report_1","report_1","report_1", "report_random_bla_foo_12321312", "report_random_bla_foo_12321312","report_random_bla_foo_12321312","report_random_bla_foo_12321312","report_random_bla_foo_12321312","report_random_bla_foo_12321312","report_random_bla_foo_12321312","report_random_bla_foo_12321312"];
Tests:
substring + extra obj
function splitCollectionMemberKey(key) { const underscoreIndex = key.indexOf('_'); if (underscoreIndex === -1) { throw new Error(`Invalid ${key} key provided, only collection keys are allowed.`); } return [key.substring(0, underscoreIndex + 1), key.substring(underscoreIndex + 1)]; } IDS.forEach((id) => splitCollectionMemberKey(id));
substring simple
function getCollectionKey(key) { if (!key) { return ''; } const lastUnderscoreIndex = key.lastIndexOf('_'); if (lastUnderscoreIndex === -1) { return key; } return key.substring(0, lastUnderscoreIndex); } IDS.forEach((id) => getCollectionKey(id));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
substring + extra obj
substring simple
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36
Browser/OS:
Chrome 126 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
substring + extra obj
4483415.0 Ops/sec
substring simple
2136983.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what is being tested, compared, and discussed. **Benchmark Definition** The benchmark definition represents two different approaches to extracting part of a string using JavaScript's `substring()` method. 1. **`splitCollectionMemberKey(key)`**: This function splits the input `key` into two parts: before and after the first underscore (`_`). It uses `indexOf()`, which searches for the first occurrence of `_` in the string, and then returns a new array with these two parts. 2. **`getCollectionKey(key)`**: This function also extracts part of a string using `substring()` but uses `lastIndexOf()` to find the last index of `_` (if it exists) and then returns only up to that index. **Comparison** The two functions are being compared in terms of performance, specifically how many times they can execute per second. The benchmark is run on a set of test cases (`IDS`) using each function. **Options Compared** Two options are being compared: 1. Using `indexOf()` and returning two parts (as in `splitCollectionMemberKey(key)`). 2. Using `lastIndexOf()` and returning only up to the last index of `_` (as in `getCollectionKey(key)`). **Pros and Cons** * **Using `indexOf()`**: Pros - simple, easy to understand; Cons - may be slower than other methods due to string search overhead. * **Using `lastIndexOf()`**: Pros - potentially faster since it only searches up to the last occurrence of `_`; Cons - may require more code complexity if not used carefully. **Other Considerations** In terms of best practice, using `indexOf()` might be considered less efficient than other methods like `startsWith()` or regular expressions, but for this specific use case, the performance difference between these two approaches is relatively small. Additionally, considering code readability and maintainability, `getCollectionKey(key)` is a more straightforward implementation. **Library Usage** There doesn't seem to be any external library used in the benchmarked code snippets, so no special libraries are being tested or compared here. **Special JS Features/Syntax** No specific JavaScript features or syntaxes are being highlighted or tested. The focus is on comparing simple string manipulation approaches between `substring()` and two different indexing methods (`indexOf()`, `lastIndexOf()`). **Alternatives** Other alternatives for extracting part of a string could include: 1. Using regular expressions (e.g., `/_+/`.exec(key)[0]) 2. Utilizing the `startsWith()` method 3. Employing other string manipulation techniques, like concatenation and indexing However, these alternatives are not being tested in this specific benchmark. Overall, this benchmark aims to compare two simple yet distinct approaches for extracting part of a string using JavaScript's `substring()` method.
Related benchmarks:
slice vs substr vs substring vs index
slice vs substr vs substring (with end index) 847
slice vs substring vs substr on 10 chars
Array split vs string substring22
Performance Test: substring vs substr vs slice 5
Comments
Confirm delete:
Do you really want to delete benchmark?