Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
String.indexOf vs String.replace
(version: 0)
Compare String.IndexOf and String.replace function
Comparing performance of:
String indexOf vs String replace
Created:
4 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var genes = '-AKR1C2--AMH--AMHR2--AR--ARX--ATRX--B9D1--CBX2--CEP41--CYB5A--CYP11A1--CYP11B1--CYP17A1--CYP19A1--DHCR7--DHH--DMRT1--DYNC2H1--FRAS1--FREM2--GATA4--GRIP1--HOXA13--HSD17B3--HSD3B2--LHCGR--MAMLD1--MAP3K1--MKKS--NEK1--NR0B1--NR3C1--NR5A1--POR--RIPK4--ROR2--RSPO1--SOX3--SOX9--SRD5A2--SRY--STAR--TCTN3--TSPYL1--DYNC2I1--WNT4--WT1--ZFPM2-'; var gene = 'LHCGR';
Tests:
String indexOf
var i = genes.indexOf('-' + gene + '-');
String replace
var i = genes.replace(/`${gene}`/i, gene);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
String indexOf
String replace
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/130.0.0.0 Safari/537.36
Browser/OS:
Chrome 130 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
String indexOf
3564042.5 Ops/sec
String replace
3121537.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **What is tested?** The provided benchmark compares two string manipulation functions in JavaScript: `String.indexOf` and `String.replace`. The test case uses a large string (`genes`) containing many substrings, including the target substring (`gene`). **Options compared:** 1. **String.indexOf**: This function returns the index of the first occurrence of the specified value (in this case, the entire `gene` string) in the original string. 2. **String.replace**: This function replaces all occurrences of a pattern with a replacement string. In this test case, the pattern is a regex that matches the entire `gene` string (`/`${gene}`/i`). The replacement string is also `gene`. **Pros and Cons:** 1. **String.indexOf**: * Pros: + Generally faster than `String.replace`, especially for small substrings. + More predictable behavior, as it only returns the first occurrence of the substring. * Cons: + May not be suitable for tasks involving multiple occurrences of a substring. 2. **String.replace**: * Pros: + Can handle multiple occurrences of a substring. + Supports regex patterns for more complex replacements. * Cons: + Generally slower than `String.indexOf` due to the additional complexity of the replacement process. **Library and its purpose:** In this benchmark, no external libraries are used. However, if we were to consider modern JavaScript features, `String.replace` uses a technique called "regex capture groups" (`/`${gene}`/i`). This allows it to extract the matched substring (in this case, `gene`) from the original string. **Special JS feature or syntax:** The use of template literals (e.g., `"var i = genes.indexOf('-' + gene + '-');"` and `"var i = genes.replace(/`${gene}`/i, gene);"`) is a modern JavaScript feature. This allows for more readable and concise code by inserting the value of an expression inside a string. **Alternatives:** If you need to compare these functions with other alternatives, consider: 1. **`String.prototype.includes()`**: A more modern alternative to `String.indexOf`, which returns `true` if the specified value is found anywhere in the original string. 2. **Regular expressions without capture groups**: You can use regular expressions without capturing groups (e.g., `/-+gene-+/`) for a simpler replacement process. Keep in mind that these alternatives might have different performance characteristics compared to `String.indexOf` and `String.replace`.
Related benchmarks:
test001
addzone_split_1
replace vs split/join v2
String.indexOf vs String.replace no-hit
Comments
Confirm delete:
Do you really want to delete benchmark?