Giter Site home page Giter Site logo

Comments (7)

Daymannovaes avatar Daymannovaes commented on July 1, 2024

Possible reasons that I could think of:

Performance

It's common sense that for statement is more performatic than forEach. But I ran some benchmarks test, and I got a surprising result. Apparently modern browsers are optimized for the second case now:

Run at https://jsbench.me/
image

Run at https://jsben.ch/
image

Compatibility

ForEach is widely implemented. It has support in 95% of all environments, against 96% of the for statement.

image
image

from events.

goto-bus-stop avatar goto-bus-stop commented on July 1, 2024

For this package the answer is "because node.js does it", and this is a straight port of the Node.js built in module. Changes are made exclusively to broaden browser support.

Your benchmark uses a single listener array of 1000 listeners which is quite unusual in the real world. 99% of the time, there will only be a small amount of listeners for an event, usually just 1. The benchmark might look different if you have less than 10 listeners vs. 1000. Though either way, this module values compatibility over performance :)

e; forEach() also doesn't have the desired behaviour on its own.

function a () { console.log('a') }
function b () { console.log('b') }
function c () { console.log('c') }
function removeB () { listeners.splice(1, 1) }
var listeners = [
  a,
  b,
  removeB,
  c
]
listeners.forEach(listener => listener())

This removes the b() handler during the iteration. But it then never calls the c listener because the indices shifted. For events it is essential that c does still get called. So the proper comparison would be to use arrayClone() and .forEach() instead.

from events.

Daymannovaes avatar Daymannovaes commented on July 1, 2024

from events.

goto-bus-stop avatar goto-bus-stop commented on July 1, 2024

Maybe my edit didn't come through email—if you use forEach() you still need to clone first.

That + the performance characteristics possibly being different for small arrays could mean it's not faster to use forEach in the typical case.

from events.

Daymannovaes avatar Daymannovaes commented on July 1, 2024

You're correct.

Although, if we clone the array first, we could still use the forEach() approach.

For example:

listeners.slice().forEach(listener => listener());

from events.

goto-bus-stop avatar goto-bus-stop commented on July 1, 2024

You could, but it's not what Node.js does, probably because it's slower.

I get this:
image

I used a short array of 3 listeners, which is probably already more than the median.

from events.

Daymannovaes avatar Daymannovaes commented on July 1, 2024

Ok, thanks for helping me.

I'll close the issue since my questions are solved.

from events.

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.