JavaScript hashing speed comparison: MD5 versus SHA-256

Hashing algorithms convert input data into a fixed-size string of characters, known as a hash value or digest. These algorithms are one-way functions, meaning the original data cannot be feasibly retrieved from the hash, which makes them useful for data integrity, password storage, and digital signatures. MD5 and SHA-256 are two such hashing algorithms with significant differences in security, speed, and application. MD5 is older and often said to faster hashing due to its simpler structure. MD5 is now considered cryptographically broken. SHA-256 provides a higher level of security with its 256-bit hash output.

But is MD5 really faster? I decided to write a little JavaScript program to hash a large input using both MD5 and SHA-256. My JavaScript code defines two functions for hashing: md5Hash uses the MD5 algorithm to produce a 128-bit hash, while sha256Hash employs SHA-256 for a more secure. A large 1GB array, created by createRandomUint8Array, is then used to benchmark these hashing functions.

function md5Hash(message) {
    return crypto.createHash('md5').update(message).digest('hex');
}

function sha256Hash(message) {
    return crypto.createHash('sha256').update(message).digest('hex');
}

const largeString = createRandomUint8Array(1000000000); // 1GB string

bench('MD5 Hash', () => {
    md5Hash(largeString);
});

bench('SHA-256 Hash', () => {
    sha256Hash(largeString);
});

I use the latest version of the Bun runtime and Node.js 23 in my tests. In find that on a ARM systems (Apple M2 or Amazon Graviton 4), Bun is slightly faster on the MD5 benchmark, but both Node.js and Bun have otherwise identical speeds.

MD5 SHA-256
Apple M2 system Bun 1.31 0.7 GB/s 2.6 GB/s
Apple M2 system Node.js 23 0.6 GB/s 2.6 GB/s
Intel Ice Lake Linux system Bun 1.31 0.7 GB/s 1.2 GB/s
Intel Ice Lake Linux system Node.js 23 0.7 GB/s 1.2 GB/s
Amazon Graviton 4 Linux system Bun 1.31 0.6 GB/s 1.8 GB/s
Amazon Graviton 4 Linux system Node.js 23 0.5 GB/s 1.8 GB/s

My results suggest that you should probably not be using MD5. MD5 is slower than SHA-256 and not as safe.

Even though SHA-256 looks more expensive on paper, modern processors typically have cryptographic extensions to accelerate it.

My code is available on GitHub.

Daniel Lemire, "JavaScript hashing speed comparison: MD5 versus SHA-256," in Daniel Lemire's blog, January 11, 2025, https://lemire.me/blog/2025/01/11/javascript-hashing-speed-comparison-md5-versus-sha-256/.

Published by

Daniel Lemire

A computer science professor at the University of Quebec (TELUQ).

5 thoughts on “JavaScript hashing speed comparison: MD5 versus SHA-256”

  1. Node palms off hashing to OpenSSL, and I’m guessing Bun is similar. So you’re really just benchmarking OpenSSL, and nothing specifically relates to Javascript.

    SHA2 is unfortunately very slow without acceleration, and whilst modern x86/ARM cores have it, it’s not exactly as widespread as I’d like yet (e.g. Skylake doesn’t support it).

    For fast cryptographic hashing, something like BLAKE3 should work well, but it’s not in OpenSSL so not widely available. You can probably get it from 3rd party modules though.

  2. Interesting! I was also curious about whether there’s much difference when hashing small amounts of data, i.e.. does either hash take longer to setup.

    On my PC (Ryzen Zen 5) both md5Hash and sha256Hash take a fixed ~340ns per call with upto about 48 bytes of input. Beyond 64 bytes the data throughput you noted starts to matter, with 256 bytes taking ~580ns for MD5 and ~420ns for SHA256.

  3. I did some benchmarks some time ago using .NET Core. SHA384 and SHA512 were consistently about twice as fast as SHA256 and SHA224 on several different x86_64 architecture machines, while they were about the same speed when benchmarking 32 bit processes.
    AFAIR, the explanation I found is that the former algorithms process 64 bit at a time, which is twice as fast if the CPU has 64 bit registers available, compared to emulating it using a pair of 32 bit registers.

  4. It’s surprising to see SHA-256 outperform MD5 in both speed and security on modern processors. With your findings, would you recommend developers completely avoid MD5 even for non-critical tasks like checksums? Also, have you considered testing newer algorithms like BLAKE3 for speed and efficiency?

Leave a Reply

Your email address will not be published.

You may subscribe to this blog by email.