Basic hash head-to-head

JavaScript performance comparison

Revision 25 of this test case created by Xotic750

Info

Tests comprise of an empty string (x 10), then the UCS2 character code range of of to 65535, split into 256 character chunks (x 256), and finally one big string of the complete UCS2 character range (x 1). Some of the functions below claim to utf8Encode the string so that they can encode it. Errors are when this encoding has failed and the function falls over or if the routine does not produce hashes that match others (eq. openssl).

btw, the utility library is work in progress and not available yet.

Preparation code

<script src="http://rghost.net/download/45222980/0355e2a8c00310e378c54b415b031cf5d92eed53/utility3.js">
</script>
<script src="//crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/md5.js">
</script>
<script src="//crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js">
</script>
<script src="//crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha256.js">
</script>
<script src="//tiny-sha1.googlecode.com/files/tinySHA1.r4.js">
</script>
<script src="//bitbucket.org/keesey/sha1/raw/fcbee69dcc6f6e9038e7d78376ab207e0816896a/bin/sha1.js">
</script>
<script src="//www.movable-type.co.uk/scripts/utf8.js">
</script>
<script src="//www.movable-type.co.uk/scripts/sha1.js">
</script>
<script src="//www.movable-type.co.uk/scripts/sha256.js">
</script>
<script src="//rghost.net/download/44852684/c85b0e31d1aba661bdb7b45e4919cbcb59c67add/sha_dev.jss">
</script>
<script src="http://www.myersdaily.org/joseph/javascript/md5.js"></script>
<script>
  var s = "",
      a = [];

  $u.range(0, 9).forEach(function() {
    a.push(s);
  });

  $u.range(1, 257).forEach(function(part) {
    s = "";
    $u.range(0, 255).forEach(function(character) {
      s += String.fromCharCode(part * character);
    });

    a.push(s);
  });

  a.push(a.join(''));
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
CryptoJs md5
a.forEach(function(str) {
  CryptoJS.MD5($u.UTF8.encode(str)).toString();
});

// handles the first 225 tests and then fails, otherwise had matching hashes, requires ucs2 to utf8 encoding.
pending…
utility md5
a.forEach(function(str) {
  $u.md5(str);
});
pending…
CryptoJs sha1
a.forEach(function(str) {
  CryptoJS.SHA1($u.UTF8.encode(str)).toString();
});

// handles the first 225 tests and then fails, otherwise had matching hashes, requires ucs2 to utf8 encoding.
pending…
utility sha1
a.forEach(function(str) {
  $u.sha1(str);
});
pending…
tiny sha1
//throw new Error("Fails to match hashes produced by other routines, unsure once ucs2 to utf8 encoded");
a.forEach(function(str) {
  SHA1($u.UTF8.encode(str));
});
pending…
movable-type sha1
a.forEach(function(str) {
  Sha1.hash(str, true);
});
pending…
other sha1
//throw new Error("Fails to match hashes produced by other routines, unsure once ucs2 to utf8 encoded");
a.forEach(function(str) {
  sha1.hash($u.UTF8.encode(str));
});
pending…
jssha sha1
a.forEach(function(str) {
  (new jsSHA($u.UTF8.encode(str), 'TEXT')).getHash('SHA-1', 'HEX');
});
pending…
CryptoJs sha256
a.forEach(function(str) {
  CryptoJS.SHA256($u.UTF8.encode(str)).toString();
});

// handles the first 225 tests and then fails, otherwise had matching hashes, requires ucs2 to utf8 encoding.
pending…
utility sha256
a.forEach(function(str) {
  $u.sha256(str);
});
pending…
movable-type sha256
a.forEach(function(str) {
  Sha256.hash(str, true);
});
pending…
jssha sha256
a.forEach(function(str) {
  (new jsSHA($u.UTF8.encode(str), 'TEXT')).getHash('SHA-256', 'HEX');
});
pending…
check sha1's
a.forEach(function(str, index) {
  var //sw1 = CryptoJS.SHA1((str)).toString(),
      //sw2 = sha1.hash(str),
      sw3 = Sha1.hash(str, true),
      //sw4 = SHA1(str),
      sw5 = $u.sha1(str),
      sw6 = (new jsSHA($u.UTF8.encode(str), 'TEXT')).getHash('SHA-1', 'HEX'),
      //t1 = sw5 === sw1,
      //t2 = sw5 === sw2,
      t3 = sw5 === sw3,
      //t4 = sw5 === sw4,
      t5 = sw5 === sw6;

  if (!t3 || !t5) {
    console.error(index, [t3, t5]);
    throw new Error("mismatch index: " + index + " " + [t3, t5].toString());
  }
});
pending…
check sha256's
a.forEach(function(str, index) {
  var //sw1 = CryptoJS.SHA256((str)).toString(),
      sw2 = Sha256.hash(str, true),
      sw3 = $u.sha256(str),
      sw4 = (new jsSHA($u.UTF8.encode(str), 'TEXT')).getHash('SHA-256', 'HEX'),
      //t1 = sw3 === sw1,
      t2 = sw3 === sw2,
      t3 = sw3 === sw4;

  if (!t2 || !t3) {
    console.error(index, [t2, t3]);
    throw new Error("mismatch index: " + index + " " + [t2, t3].toString());
  }
});
pending…
Joseph's Myers md5
a.forEach(function(str) {
  md5($u.UTF8.encode(str));
});
pending…
check md5's
a.forEach(function(str, index) {
  var //sw1 = CryptoJS.MD5((str)).toString(),
      sw2 = $u.md5(str),
      sw3 = md5($u.UTF8.encode(str)),
      //t1 = sw2 === sw1,
      t2 = sw2 === sw3;

  if (!t2) {
    console.error(index, [t2]);
    throw new Error("mismatch index: " + index + " " + [t2].toString());
  }
});
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment