Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Buf to BigInt vs BigInt to Buf
(version: 0)
Comparing performance of:
Buf to BigInt vs BigInt to Buf
Created:
one year ago
by:
Guest
Jump to the latest result
Script Preparation code:
function hashToBigInt(hash) { const view = new DataView(hash.buffer); return (view.getBigUint64(0) << 64n) | view.getBigUint64(8); } function bigIntToHash(n) { const hash = new Uint8Array(16); const view = new DataView(hash.buffer); view.setBigUint64(0, n >> 64n); view.setBigUint64(8, n); return hash; } const hashes = []; const hashInts = []; for (let i = 0; i < 1; i++) { const hash = new Uint8Array(16); window.crypto.getRandomValues(hash); hashes.push(hash); hashInts.push(hashToBigInt(hash)); } function getHashes() { return hashes; } function getHashInts() { return hashInts; }
Tests:
Buf to BigInt
for (const hash of getHashes()) { hashToBigInt(hash); }
BigInt to Buf
for (const hashInt of getHashInts()) { bigIntToHash(hashInt); }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Buf to BigInt
BigInt to Buf
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/128.0.0.0 Safari/537.36
Browser/OS:
Chrome 128 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Buf to BigInt
5184725.5 Ops/sec
BigInt to Buf
2109030.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
I'll break down the provided benchmark and explain what's being tested, compared, and considered. **Benchmark Overview** The provided benchmark tests two approaches: 1. **Buf to BigInt**: Converting a buffer (representing a hash) to a BigInt value. 2. **BigInt to Buf**: Converting a BigInt value to a buffer (representing the same hash). **Options Compared** In this benchmark, we have two main options: * **hashToBigInt**: A function that takes a buffer as input and returns a BigInt value by reading the first 8 bytes of the buffer (high-order 64-bit integer) and then shifting it left by 64n bits to combine with the next 8 bytes (low-order 64-bit integer). * **bigIntToHash**: A function that takes a BigInt value as input and returns a buffer representing the same hash. **Pros and Cons of Each Approach** 1. **hashToBigInt**: * Pros: Directly converts the buffer to a BigInt, potentially more efficient. * Cons: Requires reading from the buffer multiple times (once for each 64-bit integer), which might be slower due to memory access patterns. 2. **bigIntToHash**: * Pros: Only requires writing to the buffer once, potentially faster. * Cons: More complex conversion process, as it involves dividing the BigInt into two parts and then encoding them in the buffer. **Library Used** In this benchmark, we can see that `crypto` is used. Specifically, `getRandomValues()` is called to generate a random hash buffer. The `DataView` class is also used to read from and write to the buffer. These are part of the Web Cryptography API (W3C), which provides an interface for cryptographic operations in web applications. **Special JavaScript Feature/Syntax** The benchmark uses a feature introduced in ECMAScript 2015 (ES6): **Template literals** (`\r\nfunction hashToBigInt(hash) {\r\n \tconst view = new DataView(hash.buffer);\r\n \treturn (view.getBigUint64(0) << 64n) | view.getBigUint64(8);\r\n}\r\n\r\n...`) and **Template literals** (`\r\nfunction bigIntToHash(n) {\r\n\tconst hash = new Uint8Array(16);\r\n \tconst view = new DataView(hash.buffer);\r\n \tview.setBigUint64(0, n >> 64n);\r\n \tview.setBigUint64(8, n);\r\n \treturn hash;\r\n}\r\n...`). These features are used to simplify the code and make it more readable. **Other Alternatives** If we were to consider alternative approaches or optimizations: * **Using a dedicated library**: Instead of implementing the conversion functions ourselves, we could use an existing library that provides similar functionality. However, this might add dependencies and complexity. * **Native WebAssembly (WASM) support**: If supported by browsers, using WASM would allow for direct compilation and execution of native code, potentially providing better performance. * **Using a Just-In-Time (JIT) compiler**: Some JavaScript engines, like SpiderMonkey (used in Firefox), have JIT compilers that can optimize and compile functions at runtime. This might improve the performance of the benchmark. Keep in mind that these alternatives would require additional infrastructure or changes to the existing codebase.
Related benchmarks:
sha1asm
sha1-js ie11
JS number to UInt8Array 64-bit little endian 2
AND for VR4300
sha1-js-rusha-vs-native-10mb
Comments
Confirm delete:
Do you really want to delete benchmark?