Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Math.log2 vs Math.abs vs Math.log
(version: 0)
Comparing performance of:
Math.log2 vs Math.abs vs Math.log
Created:
one year ago
by:
Registered User
Jump to the latest result
Tests:
Math.log2
for (let i = 0; i < 100_000; i++) { let a = Math.log2(12345); }
Math.abs
for (let i = 0; i < 100_000; i++) { let a = Math.abs(12345); }
Math.log
for (let i = 0; i < 100_000; i++) { let a = Math.log(12345); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Math.log2
Math.abs
Math.log
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/144.0.0.0 Safari/537.36 OPR/128.0.0.0
Browser/OS:
Opera 128 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Math.log2
9807.9 Ops/sec
Math.abs
9793.0 Ops/sec
Math.log
9668.7 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks! **What is being tested?** The provided JSON represents three individual test cases that compare the performance of different mathematical operations: 1. `Math.log2` 2. `Math.abs` 3. `Math.log` Each test case uses a simple loop to execute the respective function (`100_000` iterations) with a fixed input value (`12345`). The goal is to determine which operation is faster. **Options compared** The three options are: 1. **Math.log2**: calculates the base-2 logarithm of a number 2. **Math.abs**: returns the absolute value of a number (i.e., its distance from zero, without considering direction) 3. **Math.log**: calculates the natural logarithm of a number **Pros and Cons of each approach:** 1. **Math.log2**: This method uses bitwise operations to calculate the logarithm, which can be more efficient than using the `Math.log` function. * Pros: often faster due to optimized implementation * Cons: may not work correctly for non-power-of-2 inputs 2. **Math.abs**: This method simply returns the absolute value of the input number without performing any arithmetic operations. * Pros: simple and easy to implement, works for all inputs * Cons: not optimized for performance, may be slower than `Math.log2` or `Math.log` 3. **Math.log**: This method uses the natural logarithm algorithm to calculate the result, which is generally considered a good balance between performance and accuracy. * Pros: accurate, widely supported, and well-tested * Cons: may not be as fast as optimized implementations like `Math.log2` **Library and purpose** There are no libraries used in these test cases. However, it's worth noting that some JavaScript environments might provide additional mathematical functions or optimizations that could affect the results. **Special JS features or syntax** None of the provided test cases use any special JavaScript features or syntax beyond the basic `for` loop. **Other alternatives** If you want to add more test cases or explore other options, here are a few ideas: * Compare performance with different mathematical libraries (e.g., `mathjs`, `numjs`) or frameworks (e.g., `TensorFlow.js`) * Test optimization techniques, such as inlining or dead code elimination * Explore the use of SIMD (Single Instruction, Multiple Data) instructions for parallel processing Keep in mind that benchmarking is an iterative process. As you refine your tests and analyze the results, you may discover opportunities to optimize your implementation further!
Related benchmarks:
math pow vs multiply (subtraction)
Math.pow(x,0.25) vs Math.sqrt(sqrt(x))
math.abs vs branchless absolute val 2
pow vs abs
Math.abs speed vs multiply
Comments
Confirm delete:
Do you really want to delete benchmark?