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
llama3.1:latest
, generated one year ago):
Let's break down the provided JSON and benchmark results. **Benchmark Definition** The benchmark is called "Float string optimization: parseFloat() vs regex, full version". This means we're comparing two different approaches to optimize float number formatting: using `parseFloat()` and using regular expressions (regex). **Test Cases** We have two individual test cases: 1. **`parseFloat()`**: In this case, the JavaScript code uses `parseFloat()` to convert a string representation of a float number (`(3/2).toFixed(4)` generates a string like "0.7500") to a numeric value using `parseFloat()`. The result is then converted back to a string with `.toString()`. 2. **`regex`**: In this case, the code uses a regular expression (regex) to remove unwanted characters from the string representation of a float number (`(3/2).toFixed(4)` generates a string like "0.7500"). The regex pattern `^[+-]?([0-9]*[.])?[0-9]+$` matches and removes any leading or trailing whitespace, as well as any decimal part (leaving only the integer part). **Library: Regular Expressions (regex)** Regular expressions are a powerful tool in JavaScript for matching patterns in strings. In this case, we're using a regex pattern to remove unwanted characters from the string representation of a float number. The regex pattern `^[+-]?([0-9]*[.])?[0-9]+$` is a bit complex, but here's what it does: * `^`: Matches the start of the string. * `[+-]?`: Matches an optional leading "+" or "-" sign (optional because it's preceded by `?`). * `([0-9]*[.])?`: Matches an optional decimal part (either a single "." followed by optional digits, or nothing). * `[0-9]+`: Matches one or more digits. * `$`: Matches the end of the string. **Pros/Cons and Considerations** The two approaches have different pros and cons: * **`parseFloat()`**: This approach is simple and easy to understand. However, it may not be as efficient as using regex because `parseFloat()` needs to create a new numeric value and then convert it back to a string. + Pros: Simple, easy to understand. + Cons: May be slower than regex. * **`regex`**: This approach is more efficient because it only involves modifying the original string without creating new values. However, regex patterns can be complex and harder to understand. + Pros: More efficient, no need to create new numeric values. + Cons: Complex regex pattern may be hard to understand. **Other Alternatives** There are other alternatives to consider: * **String manipulation**: You could simply remove unwanted characters from the string representation of a float number using string methods like `replace()` or slicing (`str.slice(0, -1)` to remove trailing decimal points). + Pros: Simple, easy to understand. + Cons: May not be as efficient as regex. Overall, the benchmark results show that the regex approach is faster than using `parseFloat()`. However, this may depend on specific use cases and JavaScript engines.
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?