DEV Community

Discussion on: ES2019 features

Collapse
 
qm3ster profile image
Mihail Malo • Edited
.flat(Infinity)

Ambitious much?

So, I had to find out.

let arr =["a single pickle"]
let i = 1
while(true){
  arr = [arr]
  console.log(i++,JSON.stringify(arr).length,arr.flat(Infinity))
}

This reliably gives out at

3910 7839 [ 'a single pickle' ]
/scrap.js:5
  console.log(i++, JSON.stringify(arr).length, arr.flat(Infinity))
                        ^

RangeError: Maximum call stack size exceeded
    at JSON.stringify (<anonymous>)

Without the stringify though?

const len = 128
let arr = ["a single pickle"]
let i = 0
while (i<74624) {
  for (let i = 0; i < len; i++) arr = [arr]
  console.log((i += len), arr.flat(Infinity))
}
while (true) {
  arr = [arr]
  console.log(++i, arr.flat(Infinity))
}
74776 [ 'a single pickle' ]
Segmentation fault (core dumped)

But what is the most nested array we can make at all?

const len = 128
let arr = ["a single pickle"]
let i = 0
while (true) {
  for (let i = 0; i < len; i++) arr = [arr]
  console.log((i += len))
}

Just runs out of default memory limit

25195520

<--- Last few GCs --->

[20300:0x442c9d0]     9849 ms: Scavenge 1364.0 (1395.7) -> 1363.2 (1396.2) MB, 1.8 / 0.0 ms  (average mu = 0.111, current mu = 0.077) allocation failure 
[20300:0x442c9d0]     9863 ms: Scavenge 1368.6 (1400.2) -> 1367.8 (1400.7) MB, 1.8 / 0.0 ms  (average mu = 0.111, current mu = 0.077) allocation failure 
[20300:0x442c9d0]     9876 ms: Scavenge 1373.3 (1405.2) -> 1372.5 (1405.7) MB, 1.9 / 0.0 ms  (average mu = 0.111, current mu = 0.077) allocation failure 


<--- JS stacktrace --->

==== JS stack trace =========================================

    0: ExitFrame [pc: 0xc738f44fb5d]
Security context: 0x346fd631d921 <JSObject>
    1: /* anonymous */ [0x2461d05931c1] [/home/mihail/Documents/Development/zigbee/zcl-packet/src/scrap.js:~1] [pc=0xc738f4f4f45](this=0x2461d05932f1 <Object map = 0x2b2b48582521>,0x2461d05932f1 <Object map = 0x2b2b48582521>,0x2461d05932b1 <JSFunction require (sfi = 0x27a794a02fa9)>,0x2461d0593229 <Module map = 0x2b2b485cc621>,0x27a794a0ac59 <String[65]: /hom...

FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
 1: 0x948d20 node::Abort() [node]
 2: 0x9499bc node::OnFatalError(char const*, char const*) [node]
 3: 0xb1160e v8::Utils::ReportOOMFailure(v8::internal::Isolate*, char const*, bool) [node]
 4: 0xb11844 v8::internal::V8::FatalProcessOutOfMemory(v8::internal::Isolate*, char const*, bool) [node]
 5: 0xf0def2  [node]
 6: 0xf0dff8 v8::internal::Heap::CheckIneffectiveMarkCompact(unsigned long, double) [node]
 7: 0xf1a718 v8::internal::Heap::PerformGarbageCollection(v8::internal::GarbageCollector, v8::GCCallbackFlags) [node]
 8: 0xf1b22b v8::internal::Heap::CollectGarbage(v8::internal::AllocationSpace, v8::internal::GarbageCollectionReason, v8::GCCallbackFlags) [node]
 9: 0xf1df61 v8::internal::Heap::AllocateRawWithRetryOrFail(int, v8::internal::AllocationSpace, v8::internal::AllocationAlignment) [node]
10: 0xee8194 v8::internal::Factory::NewFillerObject(int, bool, v8::internal::AllocationSpace) [node]
11: 0x119dccb v8::internal::Runtime_AllocateInTargetSpace(int, v8::internal::Object**, v8::internal::Isolate*) [node]
12: 0xc738f44fb5d 
Aborted (core dumped)
Collapse
 
hemanth profile image
hemanth.hm

The second issue (segfault) was fixed a few weeks back…the rest is working as intended

Collapse
 
qm3ster profile image
Mihail Malo • Edited

Just tried the segfaulting example on node v11.10.0 on Windows.
And it just quits (very early) with no error? :/

8960 [ 'a single pickle' ]
9088 [ 'a single pickle' ]

And then nothing.

Thread Thread
 
mathiasbynens profile image
Mathias Bynens

The fix Hemanth mentioned is in V8. It will become available in Node.js once they update their V8 dependency.