jsPerf.app is an online JavaScript performance benchmark test runner & jsperf.com mirror. It is a complete rewrite in homage to the once excellent jsperf.com now with hopefully a more modern & maintainable codebase.
jsperf.com URLs are mirrored at the same path, e.g:
https://jsperf.com/negative-modulo/2
Can be accessed at:
https://jsperf.app/negative-modulo/2
Fixed test case for Peter Higgins pubsub implementation. Previous tests weren't actually unsubbing properly causing slow performance.
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
/*
jQuery pub/sub plugin by Peter Higgins (dante@dojotoolkit.org)
Loosely based on Dojo publish/subscribe API, limited in scope. Rewritten blindly.
Original is (c) Dojo Foundation 2004-2010. Released under either AFL or new BSD, see:
http://dojofoundation.org/license for more information.
*/
(function($) {
var topics = {};
$.publish1 = function(topic, args) {
if (topics[topic]) {
var currentTopic = topics[topic];
for (var i = 0, j = currentTopic.length; i < j; i++) {
currentTopic[i].apply($, args || []);
}
}
};
$.subscribe1 = function(topic, callback) {
if (!topics[topic]) {
topics[topic] = [];
}
topics[topic].push(callback);
return {
"topic": topic,
"callback": callback
};
};
$.unsubscribe1 = function(handle) {
var topic = handle.topic;
if (topics[topic]) {
var currentTopic = topics[topic];
for (var i = 0, j = currentTopic.length; i < j; i++) {
if (currentTopic[i] === handle.callback) {
currentTopic.splice(i, 1);
}
}
}
};
})(jQuery);
/*!
* jQuery Tiny Pub/Sub - v0.3pre - 11/4/2010
* http://benalman.com/
*
* Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*/
/*!
* jQuery Tiny Pub/Sub - v0.X - 11/18/2010
* http://benalman.com/
*
* Original Copyright (c) 2010 "Cowboy" Ben Alman
* Dual licensed under the MIT and GPL licenses.
* http://benalman.com/about/license/
*
* Made awesome by Rick Waldron
*
*/
(function(jQuery) {
var o = jQuery({});
jQuery.each({
"subscribe2": "bind",
"unsubscribe2": "unbind",
"publish2": "trigger"
}, function(fn, api) {
jQuery[fn] = function() {
o[api].apply(o, arguments);
};
});
})(jQuery);
/*
* Simple Pub/Sub Implementation for jQuery
*
* Inspired by work from Peter Higgins (https://github.com/phiggins42/bloody-jquery-plugins/blob/master/pubsub.js)
*
* This is about the simplest way to write a pubsub JavaScript implementation for use with jQuery.
*/
(function($) {
// Cache of all topics
var topics = {};
// Iterates through all subscribers of a topic and invokes their callback,
// passing optional arguments.
$.publish3 = function(topic, args) {
if (topics[topic]) {
var thisTopic = topics[topic],
thisArgs = args || [];
for (var i = 0, j = thisTopic.length; i < j; i++) {
thisTopic[i].apply($, thisArgs);
}
}
};
// Returns a handle needed for unsubscribing
$.subscribe3 = function(topic, callback) {
if (!topics[topic]) {
topics[topic] = [];
}
topics[topic].push(callback);
return {
topic: topic,
callback: callback
};
};
// Removes the subscriber from the particular topic its handle was assigned to
$.unsubscribe3 = function(handle) {
var topic = handle.topic;
if (topics[topic]) {
var thisTopic = topics[topic];
for (var i = 0, j = thisTopic.length; i < j; i++) {
if (thisTopic[i] === handle.callback) {
thisTopic.splice(i, 1);
// break; here? duplicate handles are possible
}
}
}
};
})(jQuery);
(function(d) {
// the topic/subscription hash
var cache = {};
d.publish4 = function( /* String */ topic, /* Array? */ args) {
// summary:
// Publish some data on a named topic.
// topic: String
// The channel to publish on
// args: Array?
// The data to publish. Each array item is converted into an ordered
// arguments on the subscribed functions.
//
// example:
// Publish stuff on '/some/topic'. Anything subscribed will be called
// with a function signature like: function(a,b,c){ ... }
//
// | $.publish4("/some/topic", ["a","b","c"]);
cache[topic] && d.each(cache[topic], function() {
this.apply(d, args || []);
});
};
d.subscribe4 = function( /* String */ topic, /* Function */ callback) {
// summary:
// Register a callback on a named topic.
// topic: String
// The channel to subscribe to
// callback: Function
// The handler event. Anytime something is $.publish'ed on a
// subscribed channel, the callback will be called with the
// published array as ordered arguments.
//
// returns: Array
// A handle which can be used to unsubscribe this particular subscription.
//
// example:
// | $.subscribe4("/some/topic", function(a, b, c){ /* handle data */ });
//
if (!cache[topic]) {
cache[topic] = [];
}
cache[topic].push(callback);
return [topic, callback]; // Array
};
d.unsubscribe4 = function( /* Array */ handle) {
// summary:
// Disconnect a subscribed function for a topic.
// handle: Array
// The return value from a $.subscribe call.
// example:
// | var handle = $.subscribe("/something", function(){});
// | $.unsubscribe4(handle);
var t = handle[0];
cache[t] && d.each(cache[t], function(idx) {
if (this == handle[1]) {
cache[t].splice(idx, 1);
}
});
};
})(jQuery);
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Object cache |
| ready |
jQuery events |
| ready |
higgins inspired |
| ready |
higgins again |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.