Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Javascript RegEx Replace x ReplaceAll
(version: 0)
Comparing performance of:
Replace vs ReplaceAll
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
cnpj = "12345678901234";
Tests:
Replace
function replacer(match, p1, p2, p3, p4, p5) { let result = ''; result += p1 !== undefined ? p1 : ''; result += p2 !== undefined ? '.' + p2 : ''; result += p3 !== undefined ? '.' + p3 : ''; result += p4 !== undefined ? '/' + p4 : ''; result += p5 !== undefined ? '-' + p5 : ''; return result; } return cnpj.replace(/(\d{2})(\d{1,3})?(\d{1,3})?(\d{1,4})?(\d{1,2})?/, replacer);
ReplaceAll
cnpj.replaceAll(/(\d{2})(\d{1,3})?(\d{1,3})?(\d{1,4})?(\d{1,2})?/g, "$1.$2.$3/$4-$5");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Replace
ReplaceAll
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its test cases. **Benchmark Overview** The benchmark measures the performance of two different regular expression (Regex) replacement approaches in JavaScript: `replace()` with a custom callback function and `replaceAll()` (also known as `replaceAll()`) with a string template. **Script Preparation Code** The script preparation code is used to define a variable `cnpj` containing a fixed value, which will be used as the input string for the benchmark. The purpose of this code is to create a consistent testing scenario. **Html Preparation Code** There is no HTML preparation code provided, so we'll assume that the benchmark is focused solely on JavaScript performance. **Test Cases** The benchmark consists of two test cases: 1. **"Replace"`** * Benchmark Definition: This code defines a custom callback function `replacer` that takes six arguments (`match`, `p1`, `p2`, `p3`, `p4`, and `p5`) and returns a string by concatenating the values of these arguments. * The benchmark uses the `replace()` method to replace all occurrences of a specific Regex pattern in the `cnpj` variable. The Regex pattern is `/\\d{2}(\\d{1,3})?(\\d{1,3})?(\\d{1,4})?(\\d{1,2})?/`. * The purpose of this test case is to evaluate the performance of using a custom callback function with `replace()`. 2. **"ReplaceAll"`** * Benchmark Definition: This code uses the `replaceAll()` method (also known as `replaceAll()`) to replace all occurrences of the same Regex pattern in the `cnpj` variable. * The purpose of this test case is to evaluate the performance of using a string template with `replaceAll()`. **Options Compared** The benchmark compares two different approaches: 1. **Custom Callback Function**: Using a custom callback function with `replace()`. 2. **String Template**: Using a string template with `replaceAll()`. **Pros and Cons** Here are some pros and cons of each approach: * **Custom Callback Function (Replace)**: + Pros: Can provide more flexibility in terms of the replacement logic. + Cons: May be slower due to the overhead of executing a custom callback function for each match. * **String Template (ReplaceAll)**: + Pros: Typically faster than using a custom callback function, as it can take advantage of the browser's optimization algorithms. + Cons: Limited flexibility in terms of the replacement logic. **Library and Syntax** There is no specific library mentioned in the benchmark code. However, both `replace()` and `replaceAll()` are built-in methods in JavaScript. No special JavaScript features or syntax are used in this benchmark.
Related benchmarks:
replaceAll vs regex replace 2
replaceAll vs regex replace made in beethovben
builtin replaceAll vs regex replace
replace test 122311231 2
replaceAll vs regex replace . with ,
Comments
Confirm delete:
Do you really want to delete benchmark?