Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Number Conversion Speed 2
(version: 0)
Comparing performance of:
unary vs number vs numberNum vs int vs intNum vs float vs floatNum vs nanNull vs unaryNum
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var mb1 = 'abc'; var mb2 = '123'; var mb3 = null; var unary = (a) => { try { const b = +a; return !isNaN(b); } catch { return false; } }; var unaryNum = (a) => { try { const b = +a; return !Number.isNaN(b); } catch { return false; } }; var number = (a) => !isNaN(Number(a)); var numberNum = (a) => !Number.isNaN(Number(a)); var float = (a) => !isNaN(parseFloat(a)); var floatNum = (a) => !Number.isNaN(parseFloat(a)); var int = (a) => !isNaN(parseInt(a)); var intNum = (a) => !Number.isNaN(parseInt(a)); var nanNull = (a) => a !== null && !isNaN(a);
Tests:
unary
unary(mb1); unary(mb2); unary(mb3);
number
number(mb1); number(mb2); number(mb3);
numberNum
numberNum(mb1); numberNum(mb2); numberNum(mb3);
int
int(mb1); int(mb2); int(mb3);
intNum
intNum(mb1); intNum(mb2); intNum(mb3);
float
float(mb1); float(mb2); float(mb3);
floatNum
floatNum(mb1); floatNum(mb2); floatNum(mb3);
nanNull
nanNull(mb1); nanNull(mb2); nanNull(mb3);
unaryNum
unaryNum(mb1); unaryNum(mb2); unaryNum(mb3);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (9)
Previous results
Fork
Test case name
Result
unary
number
numberNum
int
intNum
float
floatNum
nanNull
unaryNum
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark measures the speed of various number conversion methods in JavaScript, specifically: 1. Unary operators (`+`, `!isNaN()`) 2. Number methods (`Number()`, `isNaN()`) These tests compare the performance of different approaches to check if a string represents a valid number (integer or float) using either unary operators or built-in methods. **Benchmark Definitions** The benchmark consists of eight individual test cases, each testing one specific conversion method: 1. `unary(mb1)`: Tests the unary operator approach with `+` to convert `mb1` to an integer. 2. `number(mb1)`: Tests the built-in `Number()` method to convert `mb1` to a number. 3. `numberNum(mb1)`: Tests the built-in `Number()` method and then checking if the result is NaN using `isNaN()`. 4. `int(mb1)`: Similar to `unary(mb1)`, but checks for integer conversion only. 5. `intNum(mb1)`: Similar to `numberNum(mb1)`, but checks for integer conversion after applying `Number()`. 6. `float(mb1)`: Tests the unary operator approach with `+` to convert `mb1` to a float. 7. `floatNum(mb1)`: Similar to `intNum(mb1)`, but checks for float conversion after applying `Number()`. 8. `nanNull(mb1)`: Tests if a value is NaN (Not a Number) or null. **Library and Special Cases** The benchmark uses built-in JavaScript methods (`Number()`, `isNaN()`) without relying on external libraries. However, it's worth noting that these methods can throw errors or return unexpected results for certain input values (e.g., non-numeric strings). **Performance Comparison** The provided output shows the performance measurements for each test case across multiple browser versions and platforms. The results indicate that: * Unary operator approaches (`+`, `!isNaN()`) are generally faster than built-in methods (`Number()`). * Built-in methods with subsequent checks (`numberNum(mb1)`, `floatNum(mb1)`) can be slower due to the additional overhead. * Some test cases (e.g., `nanNull(mb1)`) may have varying performance results depending on the input value and browser implementation. In summary, this benchmark highlights the importance of choosing an efficient approach for number conversion in JavaScript, taking into account both speed and robustness.
Related benchmarks:
Number constructor vs unary plus vs parseInt
Number Conversion Speed
Parse number removing insignificant digits
Formatting number, including NaN
Comments
Confirm delete:
Do you really want to delete benchmark?