Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Float string optimization: parseFloat() vs regex, full version
(version: 0)
To get a cleaned-up float number with a limited number of digits of precision, which is faster: parsefloat() or a regular expression?
Comparing performance of:
parseFloat() vs regex
Created:
5 years ago
by:
Guest
Jump to the latest result
Tests:
parseFloat()
var string = parseFloat((3/2).toFixed(4)).toString();
regex
var string = (3/2).toFixed(4).replace('^[+-]?([0-9]*[.])?[0-9]+$', '');
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
parseFloat()
regex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
parseFloat()
1703416.9 Ops/sec
regex
3463378.8 Ops/sec
Autogenerated LLM Summary
(model
gemma2:9b
, generated one year ago):
This benchmark tests two different ways to convert a floating-point number (3/2) into a string with four decimal places: using `parseFloat()` and using a regular expression. **Options Compared:** * **`parseFloat()`:** This built-in JavaScript function directly parses a string and returns the corresponding numerical value. In this case, it's used to parse the result of `(3/2).toFixed(4)`, which is already a string with four decimal places. * **Regular Expression:** This approach uses a regular expression (`^[+-]?([0-9]*[.])?[0-9]+$`) to remove any unnecessary characters from the string representation of `(3/2).toFixed(4)`. **Pros and Cons:** * **`parseFloat()`:** * **Pros:** Simpler, more concise code. * **Cons:** Might be less flexible if you need to perform more complex string manipulations before parsing. * **Regular Expression:** * **Pros:** More control over the final string format. Can handle edge cases and different input formats. * **Cons:** More complex code to write and understand, potentially slower execution due to the regex matching process. **Other Considerations:** * **Performance:** The benchmark results show that `parseFloat()` is significantly faster than using a regular expression in this specific case. This highlights the importance of benchmarking for performance optimization. * **Readability:** While both approaches work, `parseFloat()` is generally considered more readable and easier to understand. **Alternatives:** * **String Manipulation Functions:** You could use built-in JavaScript string methods like `substring`, `slice`, or `replace` to achieve a similar result as the regular expression. This might be a good compromise between performance and readability. * **Third-party Libraries:** There are libraries specifically designed for number formatting, which might offer more features and flexibility. Let me know if you have any other questions!
Related benchmarks:
Float string optimization: parseFloat() vs regex
isNaN vs regex test for stringify number check
parseFloat(toFixed) vs Math.round()
parseFloat isNaN vs RegEx parseFloat
Comments
Confirm delete:
Do you really want to delete benchmark?