DEV Community

hemanth.hm
hemanth.hm

Posted on • Edited on

ES2019 features

Cross post from h3manth.com.

So, it is time for an other post, this time it's ES2019 features.

Without wasting time with some placeholder content, the below are the features and examples:

Array#{flat,flatMap}

[1, 2, 3].flatMap((x) => [x, x * 2]);
// => [1, 2, 2, 4, 3, 6]

[1, [2, [3]]].flat(Infinity);
// => [1, 2, 3]
Enter fullscreen mode Exit fullscreen mode

Object.fromEntries

const iterableOfEntries = new Map([
    ['cat', 'dog'],
    ['life', 42]
]);
const obj = Object.fromEntries(iterableOfEntries);
console.log(obj); // { cat: "dog", life: 42 }
Enter fullscreen mode Exit fullscreen mode

String#{trimStart,trimEnd}

"    Hey JS!".trimStart(); // "Hey JS!"
    "Hey JS!    ".trimEnd(); // "Hey JS!"
Enter fullscreen mode Exit fullscreen mode

Symbol#description

const symbol = Symbol('TC39');
console.log(symbol.description); // 'TC39'
console.log(symbol.hasOwnProperty('description')); // false
Enter fullscreen mode Exit fullscreen mode

try { } catch {} // optional binding

try {
    throw new Error("End of life!");
} catch { // ✋
    console.log("^ no params for catch, you are dead anyway!");
}
Enter fullscreen mode Exit fullscreen mode

JSON ⊂ ECMAScript

// extend ECMA-262 syntax into a superset of JSON.
const LS = "";
const PS = eval("'\u2029'");
Enter fullscreen mode Exit fullscreen mode

well-formed JSON.stringify

JSON.stringify('\uD800');
// → '"\\ud800"'
Enter fullscreen mode Exit fullscreen mode

Function#toString

function /* this is bar */ bar () {}

bar.toString(); // 'function /* this is bar */ bar () {}'

// ^ perviously this was not the case.
Enter fullscreen mode Exit fullscreen mode

Array#sort stability

[
    { name: "Jan",     age: 20 },
    { name: "Jhon",    age: 20 },
    { name: "David",   age: 18 },
    { name: "Ram",     age: 18 },
    { name: "Sita",    age: 18 },
    { name: "Ravan",   age: 18 },
    { name: "Asura",   age: 12 },
    { name: "Milly",   age: 12 },
].sort((m, n) => m.age - n.age));

// People with the same age retain their order.

Enter fullscreen mode Exit fullscreen mode

Don't miss:


🎉 New JavaScript features in ES2019:

➡️ Array#{flat,flatMap}
➡️ Object.fromEntries
➡️ String#{trimStart,trimEnd}
➡️ Symbol#description
➡️ try { } catch {} // optional binding
➡️ JSON ⊂ ECMAScript
➡️ well-formed JSON.stringify
➡️ stable Array#sort
➡️ revised Function#toString

— Mathias Bynens (@mathias ) January 29, 2019

Top comments (5)

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.

Collapse
 
Sloan, the sloth mascot
Comment deleted