sdk icon indicating copy to clipboard operation
sdk copied to clipboard

[dart:js_interop] Add JSArray.from static method.

Open ykmnkmi opened this issue 1 year ago • 1 comments
trafficstars

Array.from (MDN).

ykmnkmi avatar Jun 24 '24 10:06 ykmnkmi

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.

dart-github-bot avatar Jun 24 '24 10:06 dart-github-bot

@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?

ykmnkmi avatar Aug 15 '24 12:08 ykmnkmi

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.

srujzs avatar Aug 16 '24 18:08 srujzs