CryproJS md5 & sha1 vs utility

JavaScript performance comparison

Revision 9 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). All 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. Otherwise on basic strings all of these functions provide a correct hash. For the ones that fail, they are run again, this time with the string externally utf8 encoded before being used.

Preparation code

<script src="//rghost.net/download/44832309/113f6dabf1e142fed05ba380d6f82e86a9efb17a/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>
  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(str).toString();
});
pending…
utility md5
a.forEach(function(str) {
  $u.md5(str);
});
pending…
CryptoJs sha1
a.forEach(function(str) {
  CryptoJS.SHA1(str).toString();
});
pending…
utility sha1
a.forEach(function(str) {
  $u.sha1(str);
});
pending…
tiny sha1
a.forEach(function(str) {
  SHA1(str);
});
pending…
movable-type sha1
a.forEach(function(str) {
  sha1.hash(str, true);
});
pending…
other sha1
a.forEach(function(str) {
  sha1.hash(str);
});
pending…
CryptoJs sha256
a.forEach(function(str) {
  CryptoJS.SHA256(str).toString();
});
pending…
utility sha256
a.forEach(function(str) {
  $u.sha256(str);
});
pending…
movable-type sha256
a.forEach(function(str) {
  Sha256.hash(str, true);
});
pending…
CryptoJs md5 w xtra utf8
a.forEach(function(str) {
  CryptoJS.MD5($u.utf8Encode(str)).toString();
});
pending…
CryptoJs sha1 w xtra utf8
a.forEach(function(str) {
  CryptoJS.SHA1($u.utf8Encode(str)).toString();
});
pending…
CryptoJs sha256 w xtra utf8
a.forEach(function(str) {
  CryptoJS.SHA256($u.utf8Encode(str)).toString();
});
pending…
utf8 sw1 vs sw2 check
a.forEach(function(str, index) {
  var sw1 = Utf8.encode(str),
      sw2 = $u.utf8Encode(str);

  if (sw1 !== sw2) {
    console.error(index, [str, sw1, sw2]);
    throw new Error("index: " + index + "  sw1: " + sw1 + "   sw2: " + sw2);
  }
});
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