sdk
sdk copied to clipboard
[dart:js_interop] Add JSArray.from static method.
Summary: The issue requests the addition of a JSArray.from static method to the dart:js_interop library, mirroring the functionality of JavaScript's Array.from method for creating new arrays from iterables.
@srujzs what is better or cheaper: creating a list on the Dart side and filling it with values, or creating arrays on the JS side using Array.from and converting them with .toDart?
It depends. :) One approach defers the accesses until later and the other accesses the whole array first.
When compiling to Wasm, creating an array using Array.from will be faster compared to accessing each element through interop and then putting it in a list. When compiling to JS, the performance should be closer, but Array.from is likely more optimized. toDart should also be quite fast since it's either a cast or a wrap operation.
On the other hand, accesses on the toDarted array may be slower than accesses on the Dart list you created. When compiling to JS, they will likely be the same performance, but when compiling to Wasm, you'll need to use interop for every access, so it'll be slower.