Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
get only numbers
(version: 5)
Comparing performance of:
to string - replace - and parsed vs match
Created:
8 years ago
by:
Registered User
Jump to the latest result
Tests:
to string - replace - and parsed
const str = '#10'; const strNew = parseInt(str.replace(/\D/g,''));
match
const str = '#10'; const strNew = str.toString().match(/\d+/g).map(Number);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
to string - replace - and parsed
match
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
4 months ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.127 Safari/537.36 OPR/60.3.3004.55692
Browser/OS:
Opera 60 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
to string - replace - and parsed
426397.2 Ops/sec
match
374325.4 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 Definition:** The benchmark is defined by two test cases, each with its own script preparation code and HTML preparation code (which is empty in this case). The test cases are designed to measure the performance of JavaScript microbenchmarks. **Test Cases:** 1. **"to string - replace - and parsed"**: This test case uses a simple script to: * Assign a string value `#10` to the variable `str`. * Use the `replace()` method to remove non-numeric characters (`\\D`) from the string, resulting in `10`. * Convert the modified string to a number using `parseInt()`. * Assign this new numeric value to `strNew`. 2. **"match"**: This test case uses a script to: * Assign a string value `#10` to the variable `str`. * Use the `toString()` method to convert the string to a string. * Use regular expressions (`/\\d+/g`) to match all occurrences of numeric characters in the string, resulting in an array of numbers `[10]`. * Map this array of strings to an array of numbers using `map(Number)`. **Options Compared:** The two test cases compare different approaches to achieving a similar result: 1. **"to string - replace - and parsed"**: Uses `replace()` followed by `parseInt()`, which is a straightforward approach. 2. **"match"**: Uses regular expressions to match numeric characters in the string, which can be more efficient for larger inputs. **Pros and Cons:** 1. **"to string - replace - and parsed"**: * Pros: Simple and easy to understand, works well with small inputs. * Cons: May not perform well with very large inputs due to repeated string concatenation and parsing. 2. **"match"**: * Pros: More efficient for larger inputs, as it only scans the string once and uses a regular expression cache. * Cons: Requires knowledge of regular expressions, which can be complex for beginners. **Library/Functions Used:** 1. `replace()`: A built-in JavaScript method used to replace substrings in a string. 2. `parseInt()`: A built-in JavaScript function used to convert a string to a number. 3. `toString()`: A built-in JavaScript method used to convert an object to a string. **Special JS Features/Syntax:** None mentioned, but regular expressions are used in the "match" test case. Regular expressions can be complex and may require additional explanation or expertise. **Other Alternatives:** There are other ways to achieve similar results, such as: 1. Using `RegExp` objects instead of regular expressions. 2. Using a library like `lodash` or `underscore` for more complex string manipulation tasks. 3. Using a different data structure, like an array or object, to store the numeric values. Keep in mind that these alternatives may have their own trade-offs and may not be suitable for every use case.
Related benchmarks:
Find the stray
Find the stray v2
Find the stray_eric
Number Conversion Speed
filter vs for..of vs for (i = 0..) vs preallocate
Comments
Confirm delete:
Do you really want to delete benchmark?