Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.log10() vs .toString().length
(version: 0)
Comparing performance of:
Math.log10(), .floor() vs Math.log10(), OR vs .toString().length
Created:
2 years ago
by:
Guest
Jump to the latest result
Tests:
Math.log10(), .floor()
let num = 5000; let nums = []; for(let i = 1; i <= num; ++i) { nums.push(Math.floor(Math.log10(i))); }
Math.log10(), OR
let num = 5000; let nums = []; for(let i = 1; i <= num; ++i) { nums.push(Math.log10(i) | 0); }
.toString().length
let num = 5000; let nums = []; for(let i = 1; i <= num; ++i) { nums.push(i.toString().length - 1); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.log10(), .floor()
Math.log10(), OR
.toString().length
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (iPhone; CPU iPhone OS 18_5 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/18.5 Mobile/15E148 Safari/604.1
Browser/OS:
Mobile Safari 18 on iOS 18.5
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.log10(), .floor()
14787.9 Ops/sec
Math.log10(), OR
18148.8 Ops/sec
.toString().length
4394.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down what's being tested in the provided JSON benchmark. **What is being tested?** The main goal of this benchmark is to compare the performance of three different approaches: 1. `Math.log10()` followed by `.floor()` 2. `Math.log10()` with a bitwise OR operator (`| 0`) 3. `.toString().length` (converting numbers to strings) **Options compared:** The first two options are variations of using `Math.log10()`. The difference lies in how the result is further processed: * Option 1 uses `Math.floor()` to round down the result to an integer. * Option 2 uses a bitwise OR operator (`| 0`) to truncate the decimal part of the result. The third option, `.toString().length`, converts numbers to strings and measures the length of that string. **Pros and cons:** 1. **Option 1 (Math.log10() + .floor())**: This is likely the most straightforward approach. It's simple to read and understand. However, it might incur a small overhead due to the function call. 2. **Option 2 (Math.log10() OR)**: This option uses bitwise operations, which can be faster than function calls in some cases. However, it may be less readable and more prone to errors due to the unconventional use of bitwise operators for this purpose. 3. **Option 3 (.toString().length)**: This approach has a significant overhead due to string conversion and length calculation. It's likely to be slower than both options 1 and 2. **Library and special JS features:** * No external libraries are used in these benchmarks. * There is no use of special JavaScript features like async/await, promises, or ES modules. **Other considerations:** * The benchmark uses a `for` loop to iterate from 1 to `num`, which can be slow due to the overhead of incrementing the loop variable. * The results are measured in executions per second, which is a common metric for performance benchmarks. **Alternatives:** If you want to explore alternative approaches or variations on these options, you could try: * Using other mathematical functions like `Math.log()` instead of `Math.log10()`. * Comparing the performance of different rounding modes (e.g., `Math.round()`, `Math.ceil()`, etc.). * Investigating the impact of different iteration methods (e.g., `for...of`, `while`, etc.) on performance. * Adding more complex inputs or edge cases to test the robustness of each approach. Keep in mind that optimizations like these might not be immediately apparent and may require additional investigation.
Related benchmarks:
Finding the length of a number
toFixed vs toPrecision vs Math.round() asd
parseInt(stringInt) vs +stringInt
Finding the length of a number5
toFixed vs Math.round() with numbers
Comments
Confirm delete:
Do you really want to delete benchmark?