Toggle navigation
MeasureThat
.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Hex convertion : Mapping vs Range
(version: 0)
Which one is the faster for hex conversion : testing a range of retrieving a value ? See https://url.spec.whatwg.org/#percent-encoded-bytes
Comparing performance of:
Mapping vs Range
Created:
2 years ago
by:
Guest
Go to the latest result
Script Preparation code:
a = [...Array(1024)].map(_ => Math.floor(Math.random()*256))
Tests:
Mapping
let d = 0; for (const b of a) { const c = b < 0x30 || b > 0x66 ? null // fast fail : [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, null, null, null, null, null, null, null, // :;<=>?@ 10, 11, 12, 13, 14, 15, // ABCDEF null, null, null, null, null, null, // GHIJKL null, null, null, null, null, null, null, // MNOPQRS null, null, null, null, null, null, null, // TUVWXYZ null, null, null, null, null, null, // [\]^_` 10, 11, 12, 13, 14, 15, // abcdef ][b - 0x30]; if (c !== null) { d += c; } }
Range
let d = 0; for (const b of a) { const c = b < 0x30 || b > 0x66 ? null // fast fail : b <= 0x39 // 0-9 ? b - 0x30 : b >= 0x41 && b <= 0x46 // A-F ? b - 0x37 // 0x41 - 10 : b >= 0x61 // a-f ? b - 0x57 // 0x61 - 10 : null; if (c !== null) { d += c; } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Mapping
Range
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:124.0) Gecko/20100101 Firefox/124.0
Browser/OS:
Firefox 124 on Mac OS X 10.15
View result in a separate tab
Embed
Embed Benchmark Result
Range
265K/s
Mapping
101K/s
View exact numbers
Test name
Executions per second
✓
Range
264,705 Ops/sec
Mapping
101,137 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
The provided JSON represents a benchmark test for comparing the performance of two approaches in hex conversion: mapping and range. **What is tested?** In this benchmark, a loop iterates over an array `a` containing 1024 random hexadecimal values between 0x30 (0) and 0x66 (66). The loop performs a hex conversion using one of the two approaches: 1. **Mapping**: For each value in the array, the code checks if it falls within a specific range (0-9, A-F, etc.). If it does, the corresponding ASCII character is retrieved from an object using the `b - 0x30` index. The result is stored in variable `c`. If `c` is not null, the value is added to `d`. 2. **Range**: Similar to mapping, but instead of using a specific range (0-9) and then a separate set of ranges for A-F, the code uses a single condition (`b <= 0x39`) to check if the value falls within a range (0-9), followed by another condition to check if it's in the A-F range. If it is, the corresponding ASCII character is retrieved from an object using `b - 0x37` or `b - 0x57`, depending on whether it's in the first half of the range. **Options compared** The two options being tested are: 1. **Mapping**: uses a more explicit and structured approach to hex conversion, with separate ranges for different character sets. 2. **Range**: uses a single condition to check if the value falls within a range, which may be faster but also potentially less accurate or more prone to errors. **Pros and Cons** * **Mapping**: + Pros: more explicit and structured approach, better suited for smaller ranges. + Cons: may be slower due to the overhead of checking multiple conditions. * **Range**: + Pros: potentially faster, as it eliminates the need for explicit range checks. + Cons: less accurate or more prone to errors if not implemented correctly. **Library and purpose** The `Array` object is used to create an array of 1024 random hexadecimal values. The `Math.random()` function generates these random numbers. **Special JS feature or syntax** None mentioned in the provided benchmark definition. **Other alternatives** There are other approaches that could be tested in a hex conversion benchmark, such as: 1. **Using a precomputed mapping**: creating a lookup table of ASCII characters for each range (e.g., 0-9, A-F) and using this table to perform the conversions. 2. **Using a regular expression**: converting the hexadecimal values to decimal and then using a regular expression to extract the corresponding ASCII character. 3. **Using a native JavaScript function**: if available, using a built-in JavaScript function for hex conversion, such as `String.fromCharCode()` or `parseInt()`. These alternatives would require modifications to the benchmark definition and script preparation code to accommodate the different approaches.
Related benchmarks
bitwise operator vs. boolean logic when using TypedArrays
floor() vs trunc() vs bitwise hacks (~~, >> 0, etc) 2
floor vs trunc vs bit shift
64k bytes of crypto.getRandomValues vs Math.Random (part 2)
Comments
Confirm delete:
Do you really want to delete benchmark?