MikeMcl / decimal.js

An arbitrary-precision Decimal type for JavaScript
http://mikemcl.github.io/decimal.js
MIT License
6.35k stars 480 forks source link

Decimal.sum with large arrays exceeds the maximum call stack size #230

Open mgkalogirou opened 10 months ago

mgkalogirou commented 10 months ago

Problem

The current implementation of the Decimal.sum function requires spreading the array contents, which may lead to exceeding the maximum call stack size for large arrays. This results in a breaking point, and the function fails for arrays around 70k elements.

Expected Behaviour

The Decimal.sum function should be modified to accept an array directly, rather than relying on spreading the array contents so that large arrays can be summed.

Steps to Reproduce

Create a large array, for example:

const bigArray: number[] = Array.from({ length: 100e3 }).fill(1) as number[];

Attempt to use Decimal.sum with the large array:

const decimalSum = Decimal.sum(...bigArray);

Environment

decimal.js: ^10.4.3 Node.js version: v18.17.1 TypeScript version: 5.2.2