montagejs / collections

This package contains JavaScript implementations of common data structures with idiomatic interfaces.
http://www.collectionsjs.com
Other
2.09k stars 185 forks source link

Stack overflow calling SortedSet.toArray() #164

Open royaldark opened 7 years ago

royaldark commented 7 years ago

I have a SortedSet with 9904 entries (simple one-level maps). Calling .toArray() always throws a call stack exceeded error.

 RangeError: Maximum call stack size exceeded
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:584:34)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
    at Node.reduce (C:\cygwin64\home\Joe\dev\texts\node_modules\collections\sorted-set.js:589:27)
marchant commented 7 years ago

In which JS environment, browser or node? A use case would be appreciated

kriskowal commented 7 years ago

Seems the recursive solution has outlived it usefulness. Any interest in writing a depth first traversing alternative? On Sat, Oct 15, 2016 at 12:50 AM Benoit Marchant notifications@github.com wrote:

In which JS environment, browser or node? A use case would be appreciated

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/montagejs/collections/issues/164#issuecomment-253969271, or mute the thread https://github.com/notifications/unsubscribe-auth/AADrhpBZKA4kxaSPzMEsbgjaRkcaXqB5ks5q0IXhgaJpZM4KXnMr .

royaldark commented 7 years ago

I experienced it in Node. Here is a simple reproduction:

const SortedSet = require("collections/sorted-set");

const vals = [];

for (let i = 0; i < 10000; i++) {
    vals.push(i);
}

const set = new SortedSet(vals);

console.log(set.toArray());
/home/Joe/dev/node_modules/collections/sorted-set.js:584
Node.prototype.reduce = function (callback, basis, index, thisp, tree, depth) {
                                 ^

RangeError: Maximum call stack size exceeded
    at Node.reduce (/home/Joe/dev/node_modules/collections/sorted-set.js:584:34)
    at Node.reduce (/home/Joe/dev/node_modules/collections/sorted-set.js:589:27)
    at Node.reduce (/home/Joe/dev/node_modules/collections/sorted-set.js:589:27)
    at Node.reduce (/home/Joe/dev/node_modules/collections/sorted-set.js:589:27)
    at Node.reduce (/home/Joe/dev/node_modules/collections/sorted-set.js:589:27)
    at Node.reduce (/home/Joe/dev/node_modules/collections/sorted-set.js:589:27)
    at Node.reduce (/home/Joe/dev/node_modules/collections/sorted-set.js:589:27)
    at Node.reduce (/home/Joe/dev/node_modules/collections/sorted-set.js:589:27)
    at Node.reduce (/home/Joe/dev/node_modules/collections/sorted-set.js:589:27)
    at Node.reduce (/home/Joe/dev/node_modules/collections/sorted-set.js:589:27)