Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
.split() vs .replace()
(version: 0)
test
Comparing performance of:
split vs replace
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/react/16.3.0/cjs/react.production.min.js'></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
var sample = "+50%";
Tests:
split
var result = sample.split("%")[0]
replace
var result = sample.replace(/\%/, "");
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
split
replace
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36
Browser/OS:
Chrome 145 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
split
59213356.0 Ops/sec
replace
46788356.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON and explain what is being tested, compared, and analyzed in the benchmark. **Benchmark Definition:** The benchmark compares the performance of two string manipulation operations: 1. `split()` 2. `replace()` These two methods are part of the JavaScript String prototype object. **Options Compared:** * `split()`: + Takes a regular expression as an argument (in this case, `"%"`) to split the string into substrings. + Returns an array of substrings. * `replace()`: (Note: `replace()` is actually used here with a literal character `%` instead of a regex pattern. This might be a typo in the benchmark definition.) + Takes a regular expression as an argument (in this case, `"/\\%/",` which matches the literal characters `\` and `%`) to replace occurrences of the pattern in the string. + Returns the modified string. **Pros and Cons:** * `split()`: Pros: + Simple and easy to use. + Can be used for splitting strings into multiple values. Cons: + Can be slower than `replace()` due to the overhead of creating an array of substrings. * `replace()`: Pros: + Generally faster than `split()` because it doesn't create an array of substrings. + Can be used for both replacing and inserting new characters. **Library Used:** The benchmark uses two external libraries: 1. React (version 16.3.0): A JavaScript library for building user interfaces. 2. Lodash.js (version 4.17.5): A utility library providing a wide range of functions for working with data, strings, and more. These libraries are loaded in the benchmark HTML code using `<script>` tags. **Special JS Feature/Syntax:** None. **Other Alternatives:** If you need to split or replace strings, other alternatives to `split()` and `replace()` include: * Using a loop to iterate over the string and manually process each character. * Using a regex engine like RegExp (available in older browsers) instead of Lodash's `replace()` function. However, for most use cases, `split()` and `replace()` are the most suitable and efficient choices. **Benchmark Preparation Code:** The script preparation code is used to create a sample string variable named `sample`, which contains the string `"50%"`. **Individual Test Cases:** There are two test cases: 1. "split": This test case splits the `sample` string using the `split()` method and extracts the first substring. 2. "replace": This test case replaces the `%` character in the `sample` string with nothing (`""`) using the `replace()` method. **Latest Benchmark Result:** The result shows that both tests ran for approximately 123456 executions per second, but on different browsers (Mobile Safari 10).
Related benchmarks:
Split vs Repace2
replace vs split & join
Split join vs replace
Split join vs replace (fixed string)
Comments
Confirm delete:
Do you really want to delete benchmark?