1234567213

JavaScript performance comparison

Revision 8 of this test case created by Esailija

Info

Multiple separate listeners

Preparation code

<script type="text/javascript">
var global = window;
var require = function(val) {
    if( val == "util") {
        return {
            isNumber: function( arg ) {
                 return typeof arg === 'number';
            },
            isUndefined: function( arg ) {
                return arg === void 0;
            },
            isFunction: function( arg ) {
                return typeof arg === "function";
            },
            isObject: function( arg ) {
                return typeof arg === 'object' && arg !== null;
            }
        }
    }
};
var module = {};
</script>
<script type="text/javascript" src="https://rawgithub.com/joyent/node/master/lib/events.js">
</script>
<script type="text/javascript">
var NodeEmitter = module.exports;
</script>
<script type="text/javascript" src="https://rawgithub.com/petkaantonov/FastEmitter/master/fastemitter.js">
</script>
<script type="text/javascript">
var FastEmitter = module.exports;

var a = new FastEmitter();
var b = new NodeEmitter();
if( !Array.isArray(a._events ) ) alert("modules are not loaded correctly");
if( !b._events ) alert("modules are not loaded correctly");
</script>
<script>
Benchmark.prototype.setup = function() {
        var sum = 0;
        var nodeEmitter = new NodeEmitter();
        var fastEmitter = new FastEmitter();
        var adder = function() {
            sum++;
        };
        var adder2 = function() {
            sum++;
        };
        var adder3 = function() {
            sum++;
        };
        nodeEmitter.on("eventname", adder);
        fastEmitter.on("eventname", adder);
        nodeEmitter.on("eventname2", adder);
        fastEmitter.on("eventname2", adder);
        nodeEmitter.on("eventname3", adder);
        fastEmitter.on("eventname3", adder);
   
        nodeEmitter.on("eventname", adder2);
        fastEmitter.on("eventname", adder2);
        nodeEmitter.on("eventname2", adder2);
        fastEmitter.on("eventname2", adder2);
        nodeEmitter.on("eventname3", adder2);
        fastEmitter.on("eventname3", adder2);
   
        nodeEmitter.on("eventname", adder3);
        fastEmitter.on("eventname", adder3);
        nodeEmitter.on("eventname2", adder3);
        fastEmitter.on("eventname2", adder3);
        nodeEmitter.on("eventname3", adder3);
        fastEmitter.on("eventname3", adder3);
};

Benchmark.prototype.teardown = function() {
    if( sum === 0 ) {
         throw new Error("not working");
    }
};
</script>

Preparation code output

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
node emitter
nodeEmitter.emit("eventname");
nodeEmitter.emit("eventname2");
nodeEmitter.emit("eventname3");
pending…
fast emitter
fastEmitter.emit("eventname");
fastEmitter.emit("eventname2");
fastEmitter.emit("eventname3");
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment