On June 26, 2024, the 127th Ecma General Assembly officially approved the ECMAScript 2024 language specification, marking a significant milestone in the evolution of JavaScript. This update introduces several exciting features that enhance the language’s capabilities and performance. Let’s dive into what’s new in ECMAScript 2024.
Grouping Synchronous Iterables
One of the standout features is the ability to group synchronous iterables using Map.groupBy() and Object.groupBy(). These methods allow developers to group items of an iterable into Map entries or objects based on a callback function. This functionality simplifies data manipulation and enhances code readability.
assert.deepEqual(
Map.groupBy([0, -5, 3, -4, 8, 9], x => Math.sign(x)),
new Map().set(0, [0]).set(-1, [-5, -4]).set(1, [3, 8, 9])
);
assert.deepEqual(
Object.groupBy([0, -5, 3, -4, 8, 9], x => Math.sign(x)),
{ '0': [0], '-1': [-5, -4], '1': [3, 8, 9], __proto__: null }
);
Promise.withResolvers()
The new Promise.withResolvers() method provides a streamlined way to create promises with resolvers. This feature simplifies promise management and enhances asynchronous programming in JavaScript.
const { promise, resolve, reject } = Promise.withResolvers();
Regular Expression Flag /v
ECMAScript 2024 introduces the /v flag for regular expressions, enabling advanced Unicode string property escapes and set operations for character classes. This addition improves the flexibility and power of regular expressions in JavaScript.
assert.equal(/^\p{RGI_Emoji}$/v.test('??'), true);
assert.equal(/^[\q{abc|def}]$/v.test('abc'), true);
Enhancements to ArrayBuffers and SharedArrayBuffers
ArrayBuffers now support in-place resizing and a new .transfer() method, allowing for more efficient memory management. SharedArrayBuffers can also be resized, though they can only grow and not shrink.
const buf = new ArrayBuffer(2, { maxByteLength: 4 });
const typedArray = new Uint8Array(buf, 2);
buf.resize(4);
assert.equal(typedArray.length, 2);
Ensuring Well-Formed Strings
Two new methods, String.prototype.isWellFormed() and String.prototype.toWellFormed(), help ensure that strings are well-formed with respect to UTF-16 code units. This feature enhances string handling and prevents common errors related to malformed strings.
assert.equal('abc'.isWellFormed(), true);
assert.equal('\uD800'.isWellFormed(), false);
Conclusion
ECMAScript 2024 brings a host of new features that enhance the functionality and performance of JavaScript. From grouping synchronous iterables to advanced regular expression capabilities and improved memory management, these updates make JavaScript more powerful and versatile than ever. As developers, it’s essential to stay updated with these changes to leverage the full potential of the language.
