Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Extracted number from string back to string
(version: 0)
Comparing performance of:
Splitting vs parseFloat vs regex
Created:
2 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var testString = "123.456 unit"
Tests:
Splitting
var value = testString.split(" ")[0]; var result = "Final result: " + value;
parseFloat
var value = Number.parseFloat(testString); var result = "Final result: " + value;
regex
var value = testString.match(/[-\d.]*/); var result = "Final result: " + value;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Splitting
parseFloat
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/135.0.0.0 Safari/537.36
Browser/OS:
Chrome 135 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Splitting
47943160.0 Ops/sec
parseFloat
36350968.0 Ops/sec
regex
9730914.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'd be happy to explain the JavaScript microbenchmark on MeasureThat.net. **Benchmark Overview** The benchmark measures the performance of three different approaches to extract a number from a string: splitting, using `parseFloat`, and using regular expressions. **Options Compared** 1. **Splitting**: This approach involves splitting the input string into substrings separated by whitespace characters (`\s`) using the `split()` method. The first substring is then extracted and converted back to a string. 2. **parseFloat**: This approach uses the `parseFloat()` function to convert the numeric part of the input string to a floating-point number. 3. **Regex**: This approach uses regular expressions to extract the numeric part of the input string. Specifically, it matches any characters (`[-\\d.]*`) that are either digits (`\\d`), dot (`.`), or hyphen (`-`). The matched string is then extracted and converted back to a string. **Pros and Cons** 1. **Splitting**: * Pros: Simple and straightforward. * Cons: May not work correctly if the input string contains non-whitespace characters before the numeric part, which may lead to incorrect results or errors. 2. **parseFloat**: * Pros: Robust and flexible. * Cons: May be slower than other approaches for small numbers due to the overhead of parsing a floating-point number. 3. **Regex**: * Pros: Flexible and powerful, can extract various types of numeric values (e.g., integers, floats). * Cons: Can be slower than other approaches for small numbers due to the complexity of regular expressions. **Library Used** None in this specific benchmark, as all tests are performed using built-in JavaScript functions. **Special JS Features or Syntax** There is no special JavaScript feature or syntax used in these tests. The focus is on comparing the performance of different approaches to extract a number from a string. **Other Alternatives** In addition to the three approaches mentioned above, there are other methods to extract numbers from strings, such as: 1. **Using `parseInt()`**: Similar to `parseFloat()`, but uses integer arithmetic instead. 2. **Using `match()` with a more complex regular expression**: Can be used to extract more specific types of numeric values (e.g., integers only). 3. **Using a custom parsing function**: A custom implementation that can handle various edge cases and performance optimizations. These alternatives may offer better or worse performance depending on the use case, but are not compared in this benchmark.
Related benchmarks:
Convert String to Number parseInt vs +
string to number convert
Extract number from string
Extract number from string - fixed
Comments
Confirm delete:
Do you really want to delete benchmark?