Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
parse hex-string to 'bytes like object' (Uint8Array) in javascript
(version: 0)
parse hex-string to 'bytes like object' (Uint8Array) in javascript
Comparing performance of:
bytes_fromhex1_10kb vs bytes_fromhex1_nocheck_10kb vs bytes_fromhex2_10kb
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
function makehex(bytelength=1024){ let i=-1,hex=''; while(++i<bytelength){ hex+=(Math.random()*100|0).toString(16).padStart(2,'0'); } return hex } var hex10kb=makehex(1024*10) function bytes_fromhex1(hex){//https://measurethat.net/Benchmarks/Show/107/4/parse-hex-to-bytes var bin = [], i, c, isEmpty = 1, buffer; for(i = 0; i < hex.length; i++){ c = hex.charCodeAt(i); //if(c > 47 && c < 58 || c > 64 && c < 71 || c > 96 && c < 103){ buffer = buffer << 4 ^ (c > 64 ? c + 9 : c) & 15; if(isEmpty ^= 1){ bin.push(buffer & 0xff); } //} } return new Uint8Array(bin); } function bytes_fromhex1_nocheck(s){//do some change let i=-1,n=s.length,e=1,l=new Uint8Array(n/2); let c,b; while(++i<n){ c=s.charCodeAt(i); b=b<<4^(c>64?c+9:c)&15; if(e^=1){l[i>>1]=b&0xff}; } return l } function bytes_fromhex2(input){ let i=-1,len=input.length,output=new Uint8Array(len/2); let v1,v2; while(++i<len){ v1=input.charCodeAt(i); v2=input.charCodeAt(++i); v1-=v1<58?48:v1<71?55:87; v2-=v2<58?48:v2<71?55:87; output[i>>1]=(v1<<4)+v2; }; return output } /* if((bytes_fromhex1(hex10kb).toString()==bytes_fromhex1_nocheck(hex10kb).toString())&& (bytes_fromhex2(hex10kb).toString()==bytes_fromhex1_nocheck(hex10kb).toString())) {console.log('10kbok')} */
Tests:
bytes_fromhex1_10kb
bytes_fromhex1(hex10kb);
bytes_fromhex1_nocheck_10kb
bytes_fromhex1_nocheck(hex10kb);
bytes_fromhex2_10kb
bytes_fromhex2(hex10kb);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
bytes_fromhex1_10kb
bytes_fromhex1_nocheck_10kb
bytes_fromhex2_10kb
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Benchmark Definition JSON** The benchmark definition JSON contains three test cases: * `bytes_fromhex1(hex10kb)`: This function is testing the parsing of a hexadecimal string to a Uint8Array. The input `hex10kb` is a pre-generated hexadecimal string. * `bytes_fromhex1_nocheck(hex10kb)`: This function is similar to `bytes_fromhex1`, but it doesn't perform any checks on the input. It's likely that this version is being tested for performance, while still maintaining some level of correctness. * `bytes_fromhex2(input)`: This function takes a hexadecimal string as input and returns a Uint8Array. **Options Compared** The three test cases are comparing the performance of different approaches to parse a hexadecimal string to a Uint8Array: 1. `bytes_fromhex1(hex10kb)`: This implementation uses bitwise operations to extract the binary representation of each hexadecimal digit. 2. `bytes_fromhex1_nocheck(hex10kb)`: This implementation also uses bitwise operations, but it's designed to be faster and more efficient than the previous one. 3. `bytes_fromhex2(input)`: This implementation uses a different approach, where two characters are read at a time from the input string and their binary representations are combined. **Pros and Cons** Here's a brief summary of the pros and cons of each approach: * `bytes_fromhex1(hex10kb)`: + Pros: Simple and easy to understand. + Cons: May be slower due to the use of bitwise operations and checking for valid characters. * `bytes_fromhex1_nocheck(hex10kb)`: + Pros: Optimized for performance by skipping checks on invalid characters. + Cons: May have slightly fewer correct results, as it doesn't validate the input. * `bytes_fromhex2(input)`: + Pros: Could be more efficient than the first two implementations due to its optimized binary representation extraction. + Cons: Less straightforward and less intuitive than the previous two implementations. **Library** The `Uint8Array` class is a built-in JavaScript object that represents an array of 8-bit unsigned integers. It's used as the output type for all three test cases, indicating that the benchmark is primarily concerned with performance, rather than correctness or other aspects like memory allocation. **Special JS Feature or Syntax** There doesn't seem to be any specific special feature or syntax being tested in this benchmark. **Alternatives** Other approaches could have been taken to parse a hexadecimal string to a Uint8Array: 1. Using a library like `hex-escape` (not present in the provided code) or `js-hex-encode` to handle hexadecimal encoding and decoding. 2. Implementing a custom parser using regular expressions, which would likely be less efficient than the bitwise operations used in the test cases. 3. Using WebAssembly (WASM) modules or other low-level languages to achieve better performance. Keep in mind that the choice of implementation will depend on factors like performance requirements, code readability, and maintainability.
Related benchmarks:
parse hex to bytes
array buffer to hex conversion
hex to array buffer
array buffer to hex conversion 2
Comments
Confirm delete:
Do you really want to delete benchmark?