Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Split Join vs Basic string replace
(version: 0)
Comparing performance of:
Replace (SIMPLE) vs Split and Join vs Replace (REGEX)
Created:
7 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var str = 'Abcd efghij klmnopqrstuv wxy Abcd efghij klmnopqrstuv wxy Abcd efghij klmnopqrstuv wxyAbcd efghij klmnopqrstuv wxy Abcd efghij klmnopqrstuv wxy Abcd efghij klmnopqrstuv wxy';
Tests:
Replace (SIMPLE)
str.replace("m", "$&Z");
Split and Join
str.split('').join('Z') + 'Z';
Replace (REGEX)
str.replace(/./g, "$&Z");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Replace (SIMPLE)
Split and Join
Replace (REGEX)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Mobile Safari/537.36
Browser/OS:
Chrome Mobile 136 on Android
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Replace (SIMPLE)
5394969.5 Ops/sec
Split and Join
292554.6 Ops/sec
Replace (REGEX)
221778.8 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Overview** The test compares three different approaches to replace characters in a string: 1. **Basic String Replace**: Using the `replace()` method with a simple string literal (`"m"`). 2. **Split and Join**: Splitting the string into an array using `split('')`, joining it back into a string using `'Z'`, and then concatenating `"Z"` to the end. 3. **Regex-Based Replace**: Using a regular expression with the `replace()` method, matching any character (`.`) globally (`g`) and replacing it with `$&Z`. **Options Compared** The benchmark tests these three approaches on the same input string, which contains repeated substrings of `"Abcd efghij klmnopqrstuv wxy"`. The goal is to measure the performance of each approach. **Pros and Cons of Each Approach** 1. **Basic String Replace**: This method is straightforward and easy to understand. However, it's less efficient than the other two approaches because it performs a simple string search and replacement. 2. **Split and Join**: This method can be faster than basic string replace for large strings because it avoids the overhead of regular expressions. However, it requires more memory allocations and string concatenations, which can lead to performance issues in some cases. 3. **Regex-Based Replace**: This method is more efficient than basic string replace but less efficient than split and join due to the complexity of regular expressions. The use of `$&Z` as a replacement value introduces additional overhead. **Library Used** None (native JavaScript methods) **Special JS Feature or Syntax** * `String.prototype.replace()` (basic string replace) * String concatenation (`+ 'Z'`) * Regular expression syntax (`/./g`, `$&Z`) * `String.prototype.split()` and `String.prototype.join()` **Other Considerations** The benchmark's performance results may vary depending on factors like: * Input string size * Platform (Chrome, Firefox, Safari) * Device platform (Desktop, Mobile) * Operating system When running this benchmark in your own environment, consider using a reliable and fast JavaScript engine to ensure accurate results. **Alternatives** For similar benchmarks, you can try these alternatives: 1. Benchmarking frameworks: Node.js's built-in `benchmark` module or libraries like `js-benchmark` or `BenchmarkJS`. 2. JavaScript testing tools: Jest, Mocha, or Cypress. 3. Online benchmarking platforms: BenchMarkU, micro-benchmark, or Google's Chrome DevTools (with the "Benchmark" feature).
Related benchmarks:
Regex vs split/join on replacing empty spaces
Regex vs split/join - space to dash
Regex vs split/join - space to dash 2
Regex vs split/join (remove spaces)
Comments
Confirm delete:
Do you really want to delete benchmark?