emn178/js-sha256

How fast is it compared to native ?

Closed this issue · 3 comments

Here is a lib that use native implementation from browser and node.js for sha512 :
https://github.com/paulmillr/noble-ed25519/blob/master/index.ts#L849

Browser : https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest
Node : https://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm_options

But with es module native compatibility issue. (you also have but i've handled them here : https://github.com/1000i100/js-sha256 but dropping, for now some coverage. That's why i haven't send pull-request )

My small bench for 1 000 000 sha256 :
2021-04 latest version :
TorBrowser : 51.7 s
Firefox : 46.3 s
Chromium : 23.1 s
Nodejs native crypto : 2.6 s
Nodejs js-sha256 : 1.8 s

Browser native :

const array = new Uint8Array(32);
async function bench() {
	const start = Date.now();
	for(let x = 1;x < 1000000 ; x++) {
		await crypto.subtle.digest('SHA-256', array);
	}
	const end = Date.now();
	console.log(end-start);
}
bench();

Nodejs native crypto :

import crypto from 'crypto';
const array = new Uint8Array(32);
async function bench() {
	const start = Date.now();
	for(let x = 1;x < 1000000 ; x++) {
		const sha = crypto.createHash('sha256')
		sha.update(array);
		sha.digest();
	}
	const end = Date.now();
	console.log(end-start);
}
bench();

Nodejs js-sha256 :

import sha256 from 'js-sha256';
const array = new Uint8Array(32);
async function bench() {
	const start = Date.now();
	for(let x = 1;x < 1000000 ; x++) {
		sha256(array);
	}
	const end = Date.now();
	console.log(end-start);
}
bench();

Dummy (no sha256 computing) : 0,2s

import jsSha256 from 'js-sha256';
const array = new Uint8Array(32);
async function bench() {
	const start = Date.now();
	let last;
	for(let x = 1;x < 10_000_000 ; x++) {
		fillArrayWithInt(array,x);
		//last = await jsSha256().update(array).digest();
	}
	const end = Date.now();
	console.log(end-start);
	console.log(array);
	console.log(last);
}
bench();

function fillArrayWithInt(array,int){
	for(let i =0;int>0;i++){
		array[i] = int%256;
		int = Math.trunc(int/256);
	}
}

node js-sha256 : 17s

firefox js-sha256 : 107s

import jsSha256 from 'js-sha256';
const array = new Uint8Array(32);
async function bench() {
	const start = Date.now();
	let last;
	for(let x = 1;x < 10_000_000 ; x++) {
		fillArrayWithInt(array,x);
		last = await jsSha256.update(array).digest();
	}
	const end = Date.now();
	console.log(end-start);
	console.log(array);
	console.log(last);
}
bench();

function fillArrayWithInt(array,int){
	for(let i =0;int>0;i++){
		array[i] = int%256;
		int = Math.trunc(int/256);
	}
}

native nodejs crypto : 27s

import crypto from 'crypto';
const array = new Uint8Array(32);
async function bench() {
	const start = Date.now();
	let last;
	for(let x = 1;x < 10_000_000 ; x++) {
		fillArrayWithInt(array,x);
		last = await crypto.createHash('sha256').update(array).digest();
	}
	const end = Date.now();
	console.log(end-start);
	console.log(array);
	console.log(last);
}
bench();

function fillArrayWithInt(array,int){
	for(let i =0;int>0;i++){
		array[i] = int%256;
		int = Math.trunc(int/256);
	}
}

Firefox native webcrypto : 485s

const array = new Uint8Array(32);
async function bench() {
	const start = Date.now();
	let last;
	for(let x = 1;x < 10_000_000 ; x++) {
		fillArrayWithInt(array,x);
		last = await crypto.subtle.digest('SHA-256', array);
	}
	const end = Date.now();
	console.log(end-start);
	console.log(array);
	console.log(last);
}
bench();

function fillArrayWithInt(array,int){
	for(let i =0;int>0;i++){
		array[i] = int%256;
		int = Math.trunc(int/256);
	}
}

I also added benchmark in README by using jsPerf.