How to check if two files have the same content?

for comparing large files e.g. images when asserting file uploads a comparison of buffers or strings with should.eql takes ages. i recommend asserting the buffer hash with the crypto module:

const buf1Hash = crypto.createHash('sha256').update(buf1).digest();
const buf2Hash = crypto.createHash('sha256').update(buf2).digest();
buf1Hash.should.eql(buf2Hash);

an easier approach is asserting the buffer length like so:

buf1.length.should.eql(buf2.length)

instead of using shouldjs as assertion module you can surely use a different tool


Surprisingly, no one has suggested Buffer.equals. That seems to be the fastest and simplest approach and has been around since v0.11.

So your code would become tmpBuf.equals(testBuf)