Coder Social home page Coder Social logo

mini-runner's Introduction

mini-runner

A simple alternative to events and signals with an emphasis on performance.

Can be used as an alternative to events / signals.

Build Status

How to use:

var onComplete = new MiniRunner('onComplete');

//listenerObject needs to have a 'onComplete' function
onComplete.add(listenerObject);

//emit and all listeners will have their 'onComplete' functions called
onComplete.emit(data);

Can be used to execute a funcition on many objects. Handy for games. If you need to update you game elements each frame:

var updateRunner = new MiniRunner('update');

// gameItems should all have a 'update' function
updateRunner.add(gameItem1);
updateRunner.add(gameItem2);
updateRunner.add(gameItem3);

// update game elements..
updateRunner.emit();

Features:

  • Easy to use familiar API.
  • Under the hood it dynamically creates a looping function that is highly optimised.
  • Avoids using 'call' and runs the function directly (which is faster!).
  • You can pass parameters when emitting.

Pros:

  • Doesn't rely on strings.
  • Code-completion works properly.
  • Trying to dispatch or listen to an event type that doesn't exist throws errors (helps you find errors early).
  • No need to create constants to store string values.
  • Easy to identify which signals the object dispatch.
  • Favor composition over inheritance.
  • Doesn't mess with the prototype chain.
  • Its fast, a lot faster than events and signals.
  • Great for when performance matters.
  • Its light weight, with a tiny memory footprint (smaller than events and signals)

Cons:

  • Not quite as flexible. All listeners / items in the runner must have the correct function name specified within the runners constructor.

When to use:

In practice I have found the MiniRunner increadibly useful and so thought it would be nice to share with the world. It currently forms the backbone of the messaging system in our game engine. Its working out great for things like update events, collison events etc.

Great to use if you are say looping through and array and calling the same function on each object. The resulting code is cleaner than a loop whilst still keeping the performance as fast as possible.

So yeah, if you are dispatching signals/events to a lot of listeners often (like everyframe often), then I would considor using this alternative. For most cases, this performace boost is not really important enough to switch from your current fave.

Think of this as a nice alternative for when speed really counts!

Benchmarks

benchmark results

to run the tests, move to the runner-benchmark folder then run the following:

npm run benchmark

Next open you browser (http://localhost:9966). The test is run in the console. The test result above comes from macbook pro chrome 58.

Any thoughts or comments hit me up on twitter @doormat23, I'd love to hear them!

mini-runner's People

Contributors

goodboydigital avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

mini-runner's Issues

Can't run benchmarks

Following the #benchmarks instructions to run the benchmarks results in error of

missing script: start

using
macOS 10.12.3
Node v6.9.1
NPM v3.10.8

Priorities

Hello @GoodBoyDigital. @ivanpopelyshev pointed me in the direction of this repo from this thread:
pixijs/pixijs#3570

Please see my comment here pixijs/pixijs#3570 (comment)
as this is a followup to that, just transferred to this repo.


This is any interesting approach for sure, but I would probably say the same thing I did in that comment, as this is optimizing for a particular feature of convenience, though doing a nice job of it, being that the whole concept is to allow multiple optional arguments. I wonder if you find it is needed, or if it can just handle one argument like oldskool event apis?

Also wondering if we can converge on something here with a priority-based api?

Here is a Gist of what I have for the ticker stuff I mentioned:
https://gist.github.com/drkibitz/79df1f458aa0e0db02eed338de1a25f8

Things to note:

  • Priority is only used for insertion (searches from start and end)
  • Removal is simply a filter
  • Dispatch just takes a single optional argument.

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.