bn.js: typed array vs normal array (v4)

Revision 4 of this benchmark created by Vyacheslav Egorov on


Description

Avoid polymorphic type feedback pollution between test cases by method cloning.

Preparation HTML

<script src="//cdn.rawgit.com/indutny/bn.js/v0.13.1/lib/bn.js"></script>

<script>
// Clone methods of BN to eliminate polymorphic pollution between
// different test cases (Uint32Array vs Array polymorphism).
// It only works because these methods capture no context.
//   -- @mraleph
function BNTyped(n) {
  var w = new Uint32Array(Math.max(160, n.length));
  for (var i = 0; i < w.length; i++)
    w[i] = n.words[i] | 0;

  this.sign   = n.sign;
  this.length = n.length;
  this.words = w;
}

var proto = {};
Object.keys(BN.prototype).forEach(function (key) {
  var value = BN.prototype[key];
  if (typeof value === "function") {
    value = Function("return " + value.toString())();
  }
  proto[key] = value; 
});
BNTyped.prototype = proto;
</script>

Setup

var a = new BN('c7f1bc1dfb1be82d244aef01228c1409' +
                   'c198894eca9e21430f1669b4aa3864c9' +
                   'f37f3d51b2b4ba1ab9e80f59d267fda1' +
                   '521e88b05117993175e004543c6e3611' +
                   '242f24432ce8efa3b81f0ff660b4f91c' +
                   '5d52f2511a6f38181a7bf9abeef72db0' +
                   '56508bbb4eeb5f65f161dd2d5b439655' +
                   'd2ae7081fcc62fdcb281520911d96700' +
                   'c85cdaf12e7d1f15b55ade8672407224' +
                   '25198d4ce39019550c4c8a921fc231d3' +
                   'e94297688c2d77cd68ee8fdeda38b7f9' +
                   'a274701fef23b4eaa6c1a9c15b2d77f3' +
                   '7634930386fc20ec291be95aed995680' +
                   '1e1c76601b09c413ad915ff03bfdc0b6' +
                   'b233686ae59e8caf11750b509ab4e57e' +
                   'e09202239baee3d6e392d1640185e1cd', 16);
    var b = new BN('3f70f29d3f3ae354a6d2536ceafba83c' +
                   'fc787cd91e7acd2b6bde05e62beb8295' +
                   'ae18e3f786726f8d034bbc15bf8331df' +
                   '959f59d431736d5f306aaba63dacec27' +
                   '9484e39d76db9b527738072af15730e8' +
                   'b9956a64e8e4dbe868f77d1414a8a8b8' +
                   'bf65380a1f008d39c5fabe1a9f834392' +
                   '9342ab7b4f635bdc52532d764701ff3d' +
                   '8072c475c012ff0c59373e8bc423928d' +
                   '99f58c3a6d9f6ab21ee20bc8e8818fc1' +
                   '47db09f60c81906f2c6f73dc69725f07' +
                   '5853a89f0cd02a30a8dd86b660ccdeff' +
                   'c292f398efb54088c822774445a6afde' +
                   '471f7dd327ef9996296898a5747726cc' +
                   'aeeceeb2e459df98b4128cb5ab8c7cd2' +
                   '0c563f960a1aa770f3c81f13f967b6cc', 16);
    var out = new BN(0);
    
    var ta = new BNTyped(a);
    var tb = new BNTyped(b);
    var tout = new BNTyped(out);

Test runner

Ready to run.

Testing in
TestOps/sec
array
a.mulTo(b, out);
ready
typed array
ta.mulTo(tb, tout);
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.

  • Revision 1: published by Fedor Indutny on
  • Revision 2: published by Pavan K Murthy on
  • Revision 3: published by Fedor Indutny on
  • Revision 4: published by Vyacheslav Egorov on