The Future Of JavaScript - Stage 3 Propsals |
Written by Ian Elliot | |||
Wednesday, 25 June 2025 | |||
The new proposals for ECMA Script/JavaScript have reached Stage 3, which means they will soon be with us. Is there room for excitement? After the recent revolutions in JavaScript that delivered classes and async, the latest changes have been more gradual, but we still need to keep an eye on how the language is evolving. Some of the changes are making it more useful, others are morphing it into a different language. There are nine new features being introduced, but most of them are obvious improvements to existing facilities and none of them introduce anything that you couldn't already do. Does this make them syntactic sugar? Not entirely, as some of them optimize the behavior of the language as well as making it easier to express an idea. The most interesting are: Array.fromAsync This is more direct way of creating an array from an async iterator. It is a generalization of Array.from(iterable) to async iterables: array=await Array.fromAsync(async iterable) It doesn't add anything you couldn't do before, but it is more compact. It would perhaps have been nicer to have a single from method that was both sync and async aware, but fromAsync can accept all of the argument types that from can. Decorators If you know Python you may well have encountered the idea of a decorator. This is simply the use of a function to modify what another function does, i.e. it decorates it. This is possible to do in JavaScript by passing the function into the decorator and allowing it to call the function. The new decorator allows you to do this, but with a nicer syntax, and you can more easily decorate classes, methods, getters, setters, fields and accessors. A decorator is a commonly encountered part of metaprogramming where the program modifies what parts of it do. The syntax is moderately complicated because there are many special cases, but the following example gives an impression of how things work. The decorator: function logged(value, { kind, name }) { if (kind === "method") { return function (...args) { console.log(`starting ${name} accepts a method as its first parameter and a context object as the second. The context object defines the type of decorator being defined and in this case just specifies that it is a method decorator and the current method is name. You can use the decorator to add logging to any method: class myClass{ myObject=new myClass(); This allows the logging decorator to process the method call with the result that you see: starting myMethod arguments test Examples can become much more complicated than this, but you can see the general idea. Math.sumPrecise Summing numbers is a basic part of almost every program, but most programmers don't know how to do it properly. Yes, it only involves a for loop, but the problem it overcomes is precision. If you simply loop and add then, using floating point, you lose precision. For example, if you try to add a lot of very small numbers to obtain a very big number the result can be that the small numbers are ignored, i.e. they are effectively zero, even if there are so many of them that they on their own sum to something large. The new sumPrecise method sums the values as if it was using arbitrary precision arithmetic and then converts the result back to a float. The good news is that this can be done without arbitrary precision arithmetic using a clever algorithm, see the paper Adaptive Precision Floating-Point Arithmetic. The proposal doesn't insist that this algorithm is used, but it does require that the result is as if arbitrary precision is use. The only downside is that it is slower than the usual Math.sum function which therefore still has its uses. Atomics.pause The Atomics namespace is so new you may have missed it. It contains methods that are applied to SharedArrayBuffer and ArrayBuffer in multi-processor situations to implement synchronization. For example: Atomics.wait(mySharedArray,0,42); waits, blocking the current thread until mySharedArray[0] contains 42. The new method is Atomics.pause(N) This waits for a small time period as specified by N. The exact value of N has no meaning but the pause caused is such that Pause(N)<= Pause(N+1). The wait method blocks the thread until the condition is satisfied. This can be costly in terms of what the operating system has to do and it cannot be used with the main thread as this runs the dispatcher. A blocked main thread means a frozen web page. The pause method can be used within a polling loop on a lock to give control back to the OS, i.e. as an efficient non-blocking spin lock that can be used on the main thread as well as worker threads. There are some other interesting things but their use is obvious and you can find out more at the TC39 website. To judge by the changes, JavaScript is all grown up and doesn't need major changes to make it good. But it still drifts towards what other languages have - e.g. decorators and sumPrecise like Python.
More InformationRelated ArticlesECMA Introduces More Permissive JavaScript License ECMAScript 2018 Is Feature Complete To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
Comments
or email your comment to: comments@i-programmer.info
|
|||
Last Updated ( Wednesday, 25 June 2025 ) |