Skip to content

Commit

Permalink
Optimize Array.sortWith to avoid calling the map function excesively (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mikearnaldi authored Sep 2, 2024
1 parent ab8729d commit 79859e7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/clean-meals-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"effect": patch
---

Optimize Array.sortWith to avoid calling the map function excesively
3 changes: 2 additions & 1 deletion packages/effect/src/Array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1255,7 +1255,8 @@ export const sortWith: {
<A, B>(self: Iterable<A>, f: (a: A) => B, order: Order.Order<B>): Array<A>
} = dual(
3,
<A, B>(self: Iterable<A>, f: (a: A) => B, order: Order.Order<B>): Array<A> => sort(self, Order.mapInput(order, f))
<A, B>(self: Iterable<A>, f: (a: A) => B, order: Order.Order<B>): Array<A> =>
Array.from(self).map((a) => [a, f(a)] as const).sort((a, b) => order(a[1], b[1])).map((x) => x[0])
)

/**
Expand Down

0 comments on commit 79859e7

Please sign in to comment.