Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Extract number from 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 values = testString.split(" "); var value1 = values[0];
parseFloat
var floats = Number.parseFloat(testString); var value1 = floats[0];
regex
var value1 = testString.match(/[-\d.]*/);
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:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36 Edg/143.0.0.0
Browser/OS:
Chrome 143 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Splitting
19954346.0 Ops/sec
parseFloat
9096301.0 Ops/sec
regex
13751979.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what is being tested. **Benchmark Definition** The benchmark definition provides three separate test cases: 1. **Extracting numbers from a string**: The script preparation code contains `var values = testString.split(" "); var value1 = values[0];`. This code splits the input string into an array of words, and then extracts the first word (assuming it's the number). 2. **Parsing floating-point numbers using parseFloat**: The script preparation code contains `var floats = Number.parseFloat(testString); var value1 = floats[0];`. This code attempts to parse the input string as a float using the `Number.parseFloat()` function, and then extracts the first parsed value. 3. **Extracting numbers from a string using regular expressions**: The script preparation code contains `var value1 = testString.match(/[-\\d.]*/);`. This code uses a regular expression to match one or more digits (`-`, `\d`, and `.`) in the input string, and extracts the matched value. **Options Compared** The benchmark compares three different approaches for extracting numbers from a string: * **Splitting**: Using the `split()` method to split the string into an array of words and extract the first word. * **parseFloat**: Parsing the string as a float using the `Number.parseFloat()` function. * **Regular Expressions**: Using a regular expression to match one or more digits in the input string. **Pros and Cons** 1. **Splitting**: * Pros: Simple, easy to implement, and works for simple cases where the number is followed by whitespace. * Cons: May not work correctly if the number is preceded by whitespace or other characters, and can be slower than other methods. 2. **parseFloat**: * Pros: More robust than splitting, as it can handle decimal numbers and other formats. * Cons: May not work correctly if the input string is not a valid float, and can be slower due to the parsing overhead. 3. **Regular Expressions**: * Pros: Can handle complex patterns, including decimal numbers and whitespace. * Cons: More complex to implement, and may not be as fast as other methods. **Library** None of the test cases use any external libraries. **Special JavaScript Features or Syntax** There are no specific features or syntax mentioned in the benchmark definition that would require special handling. However, it's worth noting that the regular expression used in the third test case (`/[-\\d.]*/`) uses some advanced regex features that may not be supported by all browsers. **Other Alternatives** * For extracting numbers from a string, other alternatives might include using a library like `mathjs` or `parse-number`. * For parsing floating-point numbers, other alternatives might include using the `Float32Array` or `Float64Array` types in JavaScript. * For regular expression-based extraction, other alternatives might include using a different regex pattern or exploring other matching techniques. Overall, this benchmark provides a useful comparison of three common approaches for extracting numbers from strings in JavaScript.
Related benchmarks:
Convert String to Number parseInt vs +
string to number convert
Extract number from string - fixed
Extracted number from string back to string
Comments
Confirm delete:
Do you really want to delete benchmark?