Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Sha256-Hex-Effect
(version: 0)
Comparing performance of:
window.crypto-NoHex vs window.crypto-Hex
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/sjcl/1.0.6/sjcl.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/cryptico/0.0.1343522940/cryptico.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/3.1.9-1/crypto-js.min.js"></script>
Script Preparation code:
var data = new Uint32Array(1024); window.crypto.getRandomValues(data); var dataBuffer = new Uint8Array(data); data = String.fromCharCode.apply(null, dataBuffer); // src: https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest function hex(buffer) { var hexCodes = []; var view = new DataView(buffer); for (var i = 0; i < view.byteLength; i += 4) { // Using getUint32 reduces the number of iterations needed (we process 4 bytes each time) var value = view.getUint32(i) // toString(16) will give the hex representation of the number without padding var stringValue = value.toString(16) // We use concatenation and slice for padding var padding = '00000000' var paddedValue = (padding + stringValue).slice(-padding.length) hexCodes.push(paddedValue); } // Join all the hex strings into one return hexCodes.join(""); }
Tests:
window.crypto-NoHex
crypto.subtle.digest("SHA-256", dataBuffer ).then(function (hash) {console.log(hash);});
window.crypto-Hex
crypto.subtle.digest("SHA-256", dataBuffer ).then(function (hash) {console.log(hex(hash));});
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
window.crypto-NoHex
window.crypto-Hex
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
8 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:142.0) Gecko/20100101 Firefox/142.0
Browser/OS:
Firefox 142 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
window.crypto-NoHex
394974.8 Ops/sec
window.crypto-Hex
386922.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Benchmark Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks, specifically focusing on cryptographic primitives like SHA-256. The provided benchmark definition and test cases measure the performance of the Web Cryptography API (Web Crypto) in generating and processing SHA-256 hashes. **Script Preparation Code Analysis** The script preparation code generates a random data buffer using `window.crypto.getRandomValues()` and converts it to a string using `String.fromCharCode.apply(null, dataBuffer)`. The purpose of this code is to create a test input for the Web Crypto API. Specifically, it: 1. Generates 1024 random integers using `window.crypto.getRandomValues()`. 2. Converts each integer to a hexadecimal string. 3. Concatenates these strings with padding to ensure they have a fixed length. **Library Usage** The script uses three JavaScript libraries: 1. **sjcl**: The Small JavaScript Cryptography Library (sjcl) is used for its `Digest` class, which provides an implementation of the SHA-256 algorithm. However, in this benchmark, sjcl is not actually used; instead, it's imported to show that other alternatives can be used. 2. **cryptico**: This library provides a polyfill for Web Crypto, but it's not actively maintained and has known security vulnerabilities. In this benchmark, cryptico is included as an alternative option, but its use is discouraged due to these concerns. 3. **crypto-js**: A popular JavaScript library for cryptographic primitives, including SHA-256. It's used in the `hex` function to convert the hash output from a binary format to a hexadecimal string. **Options Comparison** The benchmark compares two approaches: 1. **window.crypto-NoHex**: This option uses the Web Crypto API without converting the hash output to a hexadecimal string. 2. **window.crypto-Hex**: This option converts the hash output to a hexadecimal string using `crypto-js`. **Pros and Cons of Each Approach** **window.crypto-NoHex** Pros: * Faster performance, as it avoids the additional conversion step * More accurate results, as it doesn't introduce padding or truncation Cons: * May not be compatible with all platforms or browsers that don't support hexadecimal string conversion * Requires manual handling of binary data, which can be error-prone **window.crypto-Hex** Pros: * Easier to work with binary data, as it's converted to a human-readable format * More platform-independent, as most browsers and platforms support hexadecimal strings Cons: * Slower performance, due to the additional conversion step * May introduce padding or truncation errors, depending on the input data length **Other Alternatives** In addition to these options, other alternatives could be considered, such as using a custom implementation of SHA-256 or another cryptographic library like `webcrypto`. However, the Web Crypto API is widely supported and provides a convenient way to perform cryptographic operations in JavaScript. **Special JS Features or Syntax** None are explicitly mentioned in this benchmark.
Related benchmarks:
Sha256
Sha256-2
sha256-js
sha1-js-rusha-vs-native-10mb
Comments
Confirm delete:
Do you really want to delete benchmark?