Skip to content

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

@mgkalogirou

Description

@mgkalogirou

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions