Object.groupBy()
메소드가 추가되면서 별도의 연산을 통해 그룹화 기능이 추가되었습니다.const result = Object.groupBy([1, 2, 3, 4, 5], (num) => { return num % 2 === 0 ? 'even' : 'odd'})
// 출력: {even: [2, 4], add: [1, 3, 5]}
console.log(result)
Map.groupBy
메소드가 추가되면서 위와 동일하게 별도의 연산을 통해 그룹화 기능이 추가되었습니다.const result = Map.groupBy([1, 2, 3, 4, 5], (num) => { return num % 2 === 0 ? 'even' : 'odd'})
// 출력: Map {key: odd, value: [1, 3, 5]}, {key: even, value: [2, 4]}
console.log(result)
resolve
, reject
함수를 동시에 반환합니다. 기존에 new Promise를 통해 새로운 Promise를 만드는 과정을 간략하게 생성할 수 있게 됩니다.// new Promise를 활용한 Promise 생성
let resolve, reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
// Promise.withResolvers()를 활용한 Promise 생성
const { promise, resolve, reject } = Promise.withResolvers();
resize()
메소드를 통해 크기를 조정할 수 있습니다.const buffer = new ArrayBuffer(8, { maxByteLength: 16 });
// 출력: 8
console.log(buffer.byteLength);
buffer.resize(12);
// 출력: 12
console.log(buffer.byteLength);
transfer()
메소드를 통해 전송할 수 있게 됩니다.// ArrayBuffer 생성
const buffer = new ArrayBuffer(8);
const view = new Uint8Array(buffer);
view[1] = 2;
view[7] = 4;
// 버퍼를 같은 크기로 복사
const buffer2 = buffer.transfer();
console.log(buffer.detached); // true
console.log(buffer2.byteLength); // 8
const view2 = new Uint8Array(buffer2);
console.log(view2[1]); // 2
console.log(view2[7]); // 4
Atomics.wait
와 기능이 비슷하지만 논 블록킹으로 동작하며 메인 스레드를 사용할 수 있습니다.Int32Array
혹은 BigInt64Array
에서만 동작합니다.const sab = new SharedArrayBuffer(1024);
const int32 = new Int32Array(sab);
// index: 0, expected value: 0, timeout: 1000
// { async: true, value: Promise {<pending>} }
const result = Atomics.waitAsync(int32, 0, 0, 1000); // // result.value은 프로미스
Atomics.notify(int32, 0);
const str= '😆';
// 1글자지만 lenght는 2로 출력
console.log(str.length);
const str= '😆안녕';
for(let i=0; i< str.length; i++){
// 출력 �, �, 안, 녕
console.log(str[i]);
}
isWellFormed()
메소드를 통해 문자열이 surrogate pair 문자열인지 확인할 수 있습니다. false
를 반환하게 된다면 4바이트(2글자) 공간을 활용해야 하는 문자열인데 2바이트(1글자)만 포함되었다는 의미입니다.const str= '😆안녕';
for(let i=0; i< str.length; i++){
if(!str[i].isWellFormed()){
// 이모지에서 출력
console.error('not wellFormed');
}
console.log(str[i]);
}
toWellFormed()
메소드를 사용한다면 surrogate pair가 깨진 문자열을 U+FFFD(�)로 치환하여 출력합니다. 일반적인 브라우저는 isWellFormed()
를 사용하면 자동으로 치환해주고 있습니다.Thanks for sharing the details of the new features in ECMAScript 2024! I am impressed with the addition of features like https://sprunkiincredibox.org/ and Atomics.waitAsync(), which not only increase performance but also bring many new optimization possibilities for programmers, especially in multi-threaded and shared memory environments.
const str= '😆안녕';
👍