Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Number.toString(16) & parseInt(String, 16) vs Number.toString(36) & parseInt(String, 36)
(version: 4)
Comparing performance of:
Number.toString(16) vs Number.toString(36)
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<!--your preparation HTML code goes here-->
Script Preparation code:
const arrayNumbers = Array.from({ length: 10000 }).map(() => Math.round(Math.random() * 100000));
Tests:
Number.toString(16)
const arrayConverted16 = []; for(let i = 0; i < 10000; i++) { arrayConverted16[i] = arrayNumbers[i].toString(16); } const arrayConvertedNumFrom16 = []; for(let i = 0; i < 10000; i++) { arrayConvertedNumFrom16[i] = parseInt(arrayConverted16[i], 16); }
Number.toString(36)
const arrayConverted36 = []; for(let i = 0; i < 10000; i++) { arrayConverted36[i] = arrayNumbers[i].toString(36); } const arrayConvertedNumFrom36 = []; for(let i = 0; i < 10000; i++) { arrayConvertedNumFrom36[i] = parseInt(arrayConverted36[i], 36); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Number.toString(16)
Number.toString(36)
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/134.0.0.0 Safari/537.36
Browser/OS:
Chrome 134 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Number.toString(16)
931.4 Ops/sec
Number.toString(36)
1116.3 Ops/sec
Autogenerated LLM Summary
(model
gpt-4o-mini
, generated one year ago):
The provided benchmark tests the performance of two different numerical conversion methods in JavaScript: converting numbers to hexadecimal (base 16) and converting numbers to base 36. The benchmark specifically compares the efficiency of the built-in methods `Number.toString(base)` and `parseInt(string, base)` for each of these bases. ### Benchmark Components 1. **Preparation Phase**: The benchmark generates an array (`arrayNumbers`) filled with 10,000 random integers between 0 and 100,000. This array serves as the input for the conversion operations. 2. **Test Cases**: Each test case executes a conversion sequence: - **Base 16 Conversion**: - First, it converts numbers from `arrayNumbers` to their hexadecimal string representation using `Number.toString(16)`. - Then, it converts those strings back to numbers using `parseInt(string, 16)`. - **Base 36 Conversion**: - Similarly, it converts the same numbers to base 36 using `Number.toString(36)`. - It converts the base 36 strings back to numbers using `parseInt(string, 36)`. ### Details of the Test Cases - **Base 16 (Hexadecimal)**: - The `toString(16)` method converts a number to its hexadecimal string representation. - `parseInt(string, 16)` interprets the hexadecimal string back into a number. **Pros**: - Hexadecimal representation is compact and commonly used in programming, especially in contexts like color representation and memory addresses. **Cons**: - Hexadecimal conversion can be less efficient than other bases for certain operations, particularly when parsing large sets of numbers back into decimal. - **Base 36**: - This base encompasses digits (0-9) and letters (a-z), making it broader than base 16. - Similar to base 16, `parseInt(string, 36)` converts from base 36 back to a numeric form. **Pros**: - Base 36 handles a greater range of values in a compact string format. It might provide better performance for certain workloads due to fewer characters needed to represent large numbers. **Cons**: - Base 36 can produce more complex string representations, which may not be as familiar in certain programming contexts as base 16. ### Performance Results The benchmark results indicate the execution speed of the two methods in terms of executions per second: - **Number.toString(36)**: Achieved about **1119.36 executions/second**. - **Number.toString(16)**: Achieved about **923.35 executions/second**. These numbers suggest that converting to and from base 36 is more efficient than the hexadecimal approach for this specific operation on the given dataset. ### Other Considerations - **JavaScript Versions**: The performance may vary across different environments and versions of JavaScript engines. The results provided are based on tests run in Chrome 134 on a Windows desktop. - **Alternatives**: Other numerical bases can be considered, such as: - **Base 2 (Binary)** and **Base 8 (Octal)** for more specialized applications. - For higher-level operations, libraries like [BigInt](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt) could be utilized for handling larger integers in a way that they maintain accuracy beyond the limits of standard Number representation. However, they come with their performance implications due to their complexity. In summary, benchmarking speed and performance across different numerical base conversions can provide insights into the efficiency and practical applications for software engineers to optimize their code for various use cases.
Related benchmarks:
Array creation
clearing array
This site broken
TestArrayAllocationv2
indexOf vs map 002-5f
array.length === 0 vs !array.length
Get index with indexOf or with map
TestLoop
Array creation for, map, foreach
Comments
Confirm delete:
Do you really want to delete benchmark?