Skip to content

Commit

Permalink
bn: improve allocation speed for buffers
Browse files Browse the repository at this point in the history
Use `allocUnsafe` to avoid deprecation warnings and improve allocation
speed.

(cherry picked from commit 9f7d21f)
  • Loading branch information
chjj authored and ivanshukhov committed Jul 31, 2020
1 parent 5d2f5f4 commit f9f8487
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/bn.js
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@

this.strip();
var littleEndian = endian === 'le';
var res = new ArrayType(reqLength);
var res = allocate(ArrayType, reqLength);

var b, i;
var q = this.clone();
Expand Down Expand Up @@ -567,6 +567,13 @@
return res;
};

var allocate = function allocate (ArrayType, size) {
if (ArrayType.allocUnsafe) {
return ArrayType.allocUnsafe(size);
}
return new ArrayType(size);
};

if (Math.clz32) {
BN.prototype._countBits = function _countBits (w) {
return 32 - Math.clz32(w);
Expand Down

0 comments on commit f9f8487

Please sign in to comment.