Giter Club home page Giter Club logo

Comments (2)

betula avatar betula commented on June 27, 2024

The example above has an error. If I at first add a new listener, and at second remove the old one.

module.exports = {
  event: () => {
    let ev = (data) => {
      ev[1] ++;
      try {
          for (ev[3] = 0; i < ev[0].length; ev[3]++)
            ev[0][ev[3]](data)
      }
      finally {
          ev[1]--;
          if (!ev[1] && ev[2].length) {
            ev[0] = ev[0].concat(ev[2]);
            ev[2].length = 0;
          }
      }
    }
    ev[0] = []
    ev[1] = 0 // listen phase
    ev[2] = [] // new listeners
    ev[3] = 0 // current iteration index
    return ev
  },
  listen: (ev, fn) => (
    ev[0].indexOf(fn) < 0 && (ev[1] ? (ev[2].indexOf(fn) < 0 && ev[2].push(fn)) : ev[0].push(fn)),
    () => {
      let i = ev[0].indexOf(fn)
      i > -1 && (ev[1] && i <= ev[3] && ev[3]--, ev[0].splice(i, 1))
      i = ev[2].indexOf(fn)
      i > -1 && ev[2].splice(i, 1)
    }
  )
}

I should check its performance. Compare it with the original algorithm. We should understand how much we will miss.

from evemin.

betula avatar betula commented on June 27, 2024

50% - 100% slower

➜ evemin git:(main) ✗ node perf_algo_test.js
origin
87.53 105.24 106.76 105.22 104.91 103.82 104.55 104.74 104.72 104.75
modified
158.49 159.25 157.6 156.54 157.11 156.58 156.76 157.47 156.16 157.14
600000000
➜ evemin git:(main) ✗ bun run perf_algo_test.js
origin
56.84 52.23 69.22 52.34 50.9 52.03 51.47 52.82 51.62 52.58
modified
98.65 123.16 97.3 97.64 98.09 98.2 99.02 98.56 98.6 97.76
600000000

from evemin.

Related Issues (15)

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.