Example PR to migrate from featureEach to iterFeatures#2982
Example PR to migrate from featureEach to iterFeatures#2982
Conversation
|
cc @stebogit for input. Do you have thoughts on whether we can remove most of the functionality and just keep just the *Each functions listed above (or potentially turn them into iterator functions as demonstrated in this PR?). There are a lot of issues when trying to properly move @turf/meta to TypeScript, where the idioms are tricky to apply typings to, and it exposed some corner cases within the functions and existing typings which might require a major version to break anyhow. |
|
|
||
| const y: number[] = []; | ||
| featureEach(fc, (feature) => { | ||
| for (const { feature } of iterFeatures(fc)) { |
There was a problem hiding this comment.
This need for destructuring is a little annoying, because we have to also carry featureIndex along. But we're using featureIndex in places, and I'd want signature parity with other iterator tools that will also need to bring along their own other indexes as well.
|
Going to just go ahead and close this, generator function iteration is still massively slower than the existing methods. |

How do people feel about moving towards iterators for some of the @turf/meta functions? They were actually introduced in ES6 so we are definitely safe syntax to use. I made an example PR and migrated a few instances of
featureEachto make a little demo.Upsides:
We can introduce new iterator methods today and leave the existing methods alone (and potentially deprecate them). Relatedly, they can be written TypeScript-native from the start so we can be mindful of public API signatures and ease-of-typing issues which we're hitting in @turf/meta now.
We no longer have to check return types and break our loops, the caller can control exactly what they want.
We can stop creating any (potentially large) intermediate arrays when they aren't needed
Downsides:
I'm not sure if generators have perf parity with the existing callback situation.
It seems like we only use a small set of iterator methods really frequently: featureEach, coordEach, flattenEach, segmentEach, geomEach. The reducers are mostly just wrappers of these each methods, and could be inlined in a few places instead.