Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
"devDependencies": {
"@fast-check/vitest": "^0.2.2",
"@rollup/plugin-terser": "^0.4.4",
"@tomer/eslint-config": "^4.2.0",
"@tomer/eslint-config": "^4.3.0",
"@tomer/prettier-config": "^4.0.0",
"@vitest/coverage-v8": "^3.2.4",
"eslint": "^9.34.0",
Expand Down
10 changes: 5 additions & 5 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/internal/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@ export const mapIterable = (iterable, fn) =>

export const createAsyncIterable = fn => ({ [Symbol.asyncIterator]: fn })

export const isPromise = value =>
export const isThenable = value =>
Boolean(value && typeof value.then === `function`)

/**
* A faster version of `Promise.resolve(value).then(then)`, which is much slower
* in benchmarks because it performs native promise normalization.
*/
export const thenableThen = (value, then) =>
isThenable(value) ? value.then(then) : Promise.resolve(then(value))

export const curry = fn => {
if (fn.length <= 1 || curriedFunctions.has(fn)) {
return fn
Expand Down
18 changes: 17 additions & 1 deletion src/operations/core.bench.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { bench, describe } from 'vitest'
import { compose, pipe } from './core.js'
import { asAsync, compose, pipe } from './core.js'

const increment = (value: unknown) => Number(value) + 1

Expand All @@ -19,3 +19,19 @@ describe.each([0, 1, 2, 3, 4, 5, 6, 7, 8])(`%s function(s)`, count => {
composed(1)
})
})

describe(`iteration`, () => {
const array = Array.from({ length: 10_000 }, () => Math.random())

bench(`native`, () => {
// eslint-disable-next-line no-empty
for (const _ of array) {
}
})

bench(`asAsync`, async () => {
// eslint-disable-next-line no-empty
for await (const _ of asAsync(array)) {
}
})
})
26 changes: 19 additions & 7 deletions src/operations/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import {
createAsyncIterable,
createIterable,
identity,
isPromise,
isThenable,
thenableThen,
thunk,
} from '../internal/helpers.js'

Expand Down Expand Up @@ -62,9 +63,20 @@ export const asAsync = iterable => {
iterable[Symbol.iterator]
? // We write this instead of `() => iterable[Symbol.iterator]()` so that
// we handle the case of the iterable containing promises, each of which
// should be awaited.
async function* () {
yield* iterable
// should be awaited, and so that we are compliant with the async
// iteration protocol, which requires returning a promise.
() => {
const iterator = iterable[Symbol.iterator]()
return {
// This logic could be written more simply, but minimizing how often
// we use `await` results in more performant iteration.
next: () => {
const result = iterator.next()
return result.done
? Promise.resolve(result)
: thenableThen(result.value, value => ({ value }))
},
}
}
: async function* () {
let buffer = []
Expand Down Expand Up @@ -117,7 +129,7 @@ export const asConcur = iterable => {
const promises = []
for (const value of iterable) {
const result = safeApply(apply, value)
if (isPromise(result)) {
if (isThenable(result)) {
promises.push(result)
}
}
Expand All @@ -132,7 +144,7 @@ export const asConcur = iterable => {
const promises = []
for await (const value of iterable) {
const result = safeApply(apply, value)
if (isPromise(result)) {
if (isThenable(result)) {
promises.push(result)
}
}
Expand All @@ -141,7 +153,7 @@ export const asConcur = iterable => {
}

const safeApply = (apply, value) => {
if (isPromise(value)) {
if (isThenable(value)) {
return value.then(apply)
}

Expand Down
1 change: 1 addition & 0 deletions tsdown.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export default defineConfig([
unsafe_math: true,
},
mangle: {
eval: true,
properties: {
builtins: true,
regex: `^_[^_]+`,
Expand Down
29 changes: 29 additions & 0 deletions vitest.setup.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as matchers from 'jest-extended'
import { afterEach, beforeEach, expect, vi } from 'vitest'
import { isThenable } from './src/internal/helpers.js'
import delay from './test/delay.ts'

expect.extend(matchers)
Expand Down Expand Up @@ -104,6 +105,34 @@ expect.extend({
}
}

if (pass) {
try {
const iterator = (received as AsyncIterable<unknown>)[
Symbol.asyncIterator
]()

let result
do {
result = iterator.next()
pass = isThenable(result)
} while (pass && !(await result).done)

if (pass) {
// Once the async iterator is done, it should return that it's done
// if it's asked again, no matter how many times.
// eslint-disable-next-line require-atomic-updates
pass =
this.equals((await iterator.next()).done, true) &&
this.equals((await iterator.next()).done, true) &&
this.equals((await iterator.next()).done, true) &&
this.equals((await iterator.next()).done, true)
}
} catch {
// eslint-disable-next-line require-atomic-updates
pass = false
}
}

return {
pass,
message: (): string =>
Expand Down