var foo = Number('123.456abc');
var foo = Number.parseFloat('123.456abc');
var foo = parseFloat('123.456abc');
var foo = Number.parseInt('123.456abc');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Number | |
Number.parsefloat | |
parseFloat | |
parseInt |
Test name | Executions per second |
---|---|
Number | 15683140.0 Ops/sec |
Number.parsefloat | 10883590.0 Ops/sec |
parseFloat | 10124972.0 Ops/sec |
parseInt | 15682130.0 Ops/sec |
Overview of the Benchmark
The provided benchmark is designed to test different methods for converting string values to numbers in JavaScript. The benchmark compares the performance of Number
, parseInt
, parseFloat
, and Number.parseFloat
functions.
Options Compared
parseFloat
, but specified as a function.Pros and Cons of Each Approach
Number
due to the optional radix parameter evaluation.parseFloat
, but explicitly defined as a function, which may improve performance in some cases.parseFloat
directly.Library Usage
None of the provided test cases use any external libraries.
Special JS Features/Syntax
The benchmark uses JavaScript functions and syntax, specifically:
Number
, parseInt
, parseFloat
, and Number.parseFloat
)var foo = ...;
)'123.456abc';
)These features are standard in JavaScript and do not require special knowledge to understand.
Other Alternatives
If you want to test similar benchmarks, consider using other methods for converting string values to numbers, such as:
bigint()
: Conversions to integers or BigInts.Decimal()
libraries: Specialized libraries for decimal arithmetic, which may be faster than native JavaScript conversions.Keep in mind that the specific use case and performance characteristics of your benchmark will influence the choice of alternatives.