From 4a681f967e2bc673fb7270a6367ffc03e3839b73 Mon Sep 17 00:00:00 2001 From: Braydon Fuller Date: Wed, 1 Jul 2015 15:30:21 -0400 Subject: [PATCH] Added comment with reasoning for number or array BN instantiation. --- lib/encoding/bufferreader.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/encoding/bufferreader.js b/lib/encoding/bufferreader.js index 73f351d..d7ca157 100644 --- a/lib/encoding/bufferreader.js +++ b/lib/encoding/bufferreader.js @@ -94,6 +94,12 @@ BufferReader.prototype.readUInt64LEBN = function() { var second = this.buf.readUInt32LE(this.pos); var first = this.buf.readUInt32LE(this.pos + 4); var combined = (first * 0x100000000) + second; + // Instantiating an instance of BN with a number is faster than with an + // array or string. However, the maximum safe number for a double precision + // floating point is 2 ^ 52 - 1 (0x1fffffffffffff), thus we can safely use + // non-floating point numbers less than this amount (52 bits). And in the case + // that the number is larger, we can instatiate an instance of BN by passing + // an array from the buffer (slower) and specifying the endianness. var bn; if (combined <= 0x1fffffffffffff) { bn = new BN(combined);