Coder Social home page Coder Social logo

Why not `for of`? about ts-lazy-collections HOT 4 OPEN

qm3ster avatar qm3ster commented on July 21, 2024
Why not `for of`?

from ts-lazy-collections.

Comments (4)

dvester avatar dvester commented on July 21, 2024 1

So I tested the difference between using for of, and interacting with the iterator via next() and the difference was huge. In favor of the direct interaction. Using the for of was significantly slower for even small arrays. It was odd because when using for of it didn't seem to benefit from the deferred execution at all. So as the original array size increased, so did the time it took to execute the method chain. The same was not true when using direct interaction with the next() function.

from ts-lazy-collections.

WimJongeneel avatar WimJongeneel commented on July 21, 2024

I didn't run any tests, but my gut says:

  • on small collections this is faster due to having less iterators (which have a considerable overhead)
  • on big collections it is slower because the ... operator (shallowly) clones all the items in the collection which at some point will cost more resources then the gains of having less iterators

But this is just speculation, the only way to know for sure it to test a variety of cases. Properly will also depend on the runtime you are using as iterators are a recent addition and I expect more modern runtimes to have more efficient implementations of them.

from ts-lazy-collections.

qm3ster avatar qm3ster commented on July 21, 2024

Not sure what you mean, I didn't really use ... much. Only in arguments, but I could as well take arrays in those two cases.

Oh, if you mean c the currying function, then sure, it's probably bad news, and I should just rewrite the functions as eg

const filter = p => function* filter(it) {
  for (const x of it) if (p(x)) yield fn(x);
}

But I meant the fact that you manually called .next() a lot.
I'd assume the for optimizes better, even for manual implementations of iterator?

from ts-lazy-collections.

XantreDev avatar XantreDev commented on July 21, 2024

Lets make benchmark

from ts-lazy-collections.

Related Issues (1)

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.