trxcllnt commented on code in PR #45251:
URL: https://github.com/apache/arrow/pull/45251#discussion_r1920515540


##########
js/src/util/bn.ts:
##########
@@ -91,15 +91,25 @@ export function bigNumToNumber<T extends 
BN<BigNumArray>>(bn: T, scale?: number)
             number |= word * (BigInt(1) << BigInt(64 * i++));
         }
     }
-    if (typeof scale === 'number') {
-        const denominator = BigInt(Math.pow(10, scale));
+    if (typeof scale === 'number' && scale > 0) {
+        const denominator = BigInt('1' + '0'.repeat(scale));
         const quotient = number / denominator;
-        const remainder = number % denominator;
-        return bigIntToNumber(quotient) + (bigIntToNumber(remainder) / 
bigIntToNumber(denominator));
+        const remainder = negative? -(number % denominator) : number % 
denominator;
+        const integerPart = bigIntToNumber(quotient);
+        const fractionPart = padStart((remainder).toString(), scale);
+        const sign: string = negative && integerPart === 0 ? '-' : '';
+        return Number(`${sign}${integerPart}.${fractionPart}`);

Review Comment:
   IIRC coercing via `+str` is a bit faster than using the Number constructor. 
Minifiers will typically do this as well:
   
   ```suggestion
           return +`${sign}${integerPart}.${fractionPart}`;
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to