GithubHelp home page GithubHelp logo

Removing async/await code about iter-ops HOT 10 CLOSED

vitaly-t avatar vitaly-t commented on June 17, 2024
Removing async/await code

from iter-ops.

Comments (10)

vitaly-t avatar vitaly-t commented on June 17, 2024 1

Thank you for looking into this!

The priority here is to get rid of the async/await syntax, because it slows things down quite a bit, or at least according to some tests I did in the past.

However, the whole issue isn't really a big deal, certainly not one to justify another major change. That's why I'd prefer to leave it without making any such big change, at least presently.

I appreciate all the work you've been putting into this! And since you like good challenges, then issue #33 is certainly worth looking into. It is the most complex operator that we have, and I believe some function there is off, along with the tests for it.

from iter-ops.

RebeccaStevens avatar RebeccaStevens commented on June 17, 2024

I'm actually kind of curious whether using callbags would be the best approach. They're super performant and might even be able to dedup a lot of commonalities between the sync and async code.

from iter-ops.

vitaly-t avatar vitaly-t commented on June 17, 2024

I do not see how callbacks can be used there, since every operator is expected to return a Promise.

from iter-ops.

RebeccaStevens avatar RebeccaStevens commented on June 17, 2024

Checkout this talk: https://www.youtube.com/watch?v=Ir9-EBbc9fg

from iter-ops.

vitaly-t avatar vitaly-t commented on June 17, 2024

It would take a bit of research to see if that would even be possible to use, given this library's objectives and current function. And if it were, this issue here is about the implementation aspect, strictly. I prefer to keep it simple.

from iter-ops.

RebeccaStevens avatar RebeccaStevens commented on June 17, 2024

I've done a bit of testing on whether using callbags would be a viable approach.

Here's how a couple of the operations would look:

export const aggregate =
    <A, R>(cb: (a: A[]) => R) =>
    async (self: Source<A, unknown>): Promise<R> =>
        cb(await asyncIterableToArray(toAsyncIterable(self)));

export const count =
    <A>(cb: (value: A, index: number) => Promise<number> | number) =>
    async (self: Source<A, unknown>): Promise<number> => {
        let count = 0;
        let index = 0;
        for await (const element of toAsyncIterable(self)) {
            const result = await cb(element, index++);
            if (result) {
                count++;
            }
        }
        return count;
    };

Of course, async/await can be removed from this code but it won't be as elegant.
Those implementations above can be used from either a sync or async starting point.
A slight performance increase for sync can be achieved with a dedicated sync version of these operations:

export const aggregateSync =
    <A, R>(cb: (a: A[]) => R) =>
    (self: Source<A, unknown>): R =>
        cb([...toIterable(self)]);

export const countSync =
    <A>(cb: (value: A, index: number) => boolean) =>
    (self: Source<A, unknown>): number => {
        let count = 0;
        let index = 0;
        for (const element of toIterable(self)) {
            const result = cb(element, index++);
            if (result) {
                count++;
            }
        }
        return count;
    };

Callbags will also allow for more power pipelines.
For example, here pipe returns a Promise<number>.

const output = await pipe(
    fromIter([1, 2, 3]),
    aggregate((arr) => {
        return arr.reduce((a, c) => a + c);
    })
);
expect(output).to.eql(6);

And with the explicit sync operator, pipe returns a number.

const output = pipe(
    fromIter([1, 2, 3]),
    aggregateSync((arr) => {
        return arr.reduce((a, c) => a + c);
    })
);
expect(output).to.eql(6);

I believe using callbags would be quite beneficial but would take a bit of work to implement (and will of course be a breaking change).

Let me know if you want me to continue to pursue this.

from iter-ops.

vitaly-t avatar vitaly-t commented on June 17, 2024

Finished rewriting operator concat, merged into master.

from iter-ops.

vitaly-t avatar vitaly-t commented on June 17, 2024

P.S. Oddly enough, my WebStorm UI fails to detect iterable type correctly when we have more than 2 concat parameters, as shown below... even though our own test seems to pass there.

image

from iter-ops.

vitaly-t avatar vitaly-t commented on June 17, 2024

Version 2.3.2 now has new version of concat.

from iter-ops.

vitaly-t avatar vitaly-t commented on June 17, 2024

Version 2.3.3 now has new version of defaultEmpty.

from iter-ops.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.