Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Reverse a number
(version: 0)
Which is the faster way to reverse a number in JS?
Comparing performance of:
reverseNum numerically vs The String way
Created:
7 years ago
by:
Guest
Jump to the latest result
Tests:
reverseNum numerically
let num = 456 var r = 0; while (num) { r *= 10; r += num % 10; num = Math.floor(num / 10); } console.log(r)
The String way
let num = 456 Number(num.toString().split('').reverse().join('')) console.log(num)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
reverseNum numerically
The String way
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one month ago
)
User agent:
Mozilla/5.0 (X11; Linux x86_64; rv:143.0) Gecko/20100101 Firefox/143.0
Browser/OS:
Firefox 143 on Linux
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
reverseNum numerically
165601.4 Ops/sec
The String way
160650.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net. **Benchmark Definition** The provided benchmark definition represents two test cases: reversing a number using numerical methods and using strings to reverse the number. * The first test case, "reverseNum numerically," uses a while loop to extract each digit from the input number `num` by multiplying the current value of `r` by 10, adding the remainder of `num` divided by 10, and then updating `num` to be the integer division of the original `num` by 10. This process is repeated until there are no more digits left in `num`. * The second test case, "The String way," converts the input number to a string using `toString()`, splits the string into an array of characters, reverses the array using the `reverse()` method, and then joins the characters back together into a single string. **Options Compared** In this benchmark, we have two main options: 1. **Numerical Method**: This approach uses a while loop to extract each digit from the input number. 2. **String-Based Approach**: This approach converts the input number to a string and then reverses the string using the `reverse()` method. **Pros and Cons of Each Approach** * **Numerical Method** * Pros: * More efficient for very large numbers, as it avoids the overhead of creating a new string. * Less memory-intensive, as it only requires storing a single integer value (`r`). * Cons: * May be slower due to the use of a while loop and arithmetic operations. * **String-Based Approach** * Pros: * Can be faster for small numbers, as it avoids the overhead of arithmetic operations. * Often more readable and easier to understand, especially for developers without deep knowledge of numerical algorithms. * Cons: * Less efficient for very large numbers due to the creation of a new string and the `reverse()` method. * More memory-intensive, as it requires storing an additional string. **Library Used** In this benchmark, no libraries are explicitly mentioned. However, the `Number()` function used in the second test case is part of the JavaScript standard library. **Special JS Feature or Syntax** The use of arrow functions (`=>`) is not present in this benchmark. However, some other features like template literals (`\r\n`) and `let`/`const` declarations are used to define the script preparation code for each test case. **Other Alternatives** If you were to consider alternative approaches to reversing a number, here are a few options: * **Recursive Approach**: You could use recursion to reverse the number by calling a function with the last digit of `num` and incrementing `num` until there's only one digit left. * **Bit Manipulation**: For very large numbers, you could use bit manipulation techniques like bitwise XOR and shifts to extract each digit from the input number. * **BigInt**: If you're working with very large integers in modern JavaScript ( ECMAScript 2020 and later), you can use the `BigInt` type to represent arbitrary-precision integers. Keep in mind that these alternative approaches might have their own trade-offs in terms of performance, readability, and memory usage.
Related benchmarks:
reverse number
Float string optimization: parseFloat() vs regex, full version
Number Conversion Speed
bignumber.js vs native benchmark: compareTo
Comments
Confirm delete:
Do you really want to delete benchmark?