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
Added pubsub implementation using jQuery 1.7 $.Callbacks. Inspired by http://addyosmani.com/blog/jquery-1-7s-callbacks-feature-demystified/ .
Ben Alman's - v0.7
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
;
(function(d) {
// the topic/subscription hash
var cache = {};
d.publish1 = function( /* String */ topic, /* Array? */ args) {
cache[topic] && d.each(cache[topic], function() {
this.apply(d, args || []);
});
};
d.subscribe1 = function( /* String */ topic, /* Function */ callback) {
if (!cache[topic]) {
cache[topic] = [];
}
cache[topic].push(callback);
return [topic, callback]; // Array
};
d.unsubscribe1 = function( /* Array */ handle) {
var t = handle[0];
cache[t] && d.each(cache[t], function(idx) {
if (this == handle[1]) {
cache[t].splice(idx, 1);
}
});
};
})(jQuery);
/* jQuery Tiny Pub/Sub - v0.7 - 10/27/2011
* http://benalman.com/
* Copyright (c) 2011 "Cowboy" Ben Alman; Licensed MIT, GPL */
(function($) {
var o = $({});
$.subscribe2 = function() {
o.on.apply(o, arguments);
};
$.unsubscribe2 = function() {
o.off.apply(o, arguments);
};
$.publish2 = function() {
o.trigger.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) {
var cache = {};
d.publish4 = function(topic, args) {
if (cache[topic]) {
var currentCache = cache[topic],
currentArgs = args || [];
for (var i = 0, j = currentCache.length; i < j; i++) {
currentCache[i].apply(d, currentArgs);
}
}
};
d.subscribe4 = function(topic,callback) {
if (!cache[topic]) {
cache[topic] = [];
}
cache[topic].push(callback);
return [topic, callback];
};
d.unsubscribe4 = function(handle) {
var topic = handle[0];
if (cache[topic]) {
var currentCache = cache[topic];
for (var i = 0, j = currentCache.length; i < j; i++) {
if (currentCache[i] === handle[1]) {
currentCache.splice(i, 1);
}
}
}
};
})(jQuery);
/*
using jquery 1.7 $.Callback
*/
(function(jQuery) {
var topics = {};
jQuery.Topic = function( id ) {
var callbacks,
method,
topic = id && topics[ id ];
if ( !topic ) {
callbacks = jQuery.Callbacks();
topic = {
publish: callbacks.fire,
subscribe: callbacks.add,
unsubscribe: callbacks.remove
};
if ( id ) {
topics[ id ] = topic;
}
}
return topic;
};
})(jQuery);
</script>
Ready to run.
Test | Ops/sec | |
---|---|---|
Object cache (Higgins) |
| ready |
jQuery events |
| ready |
higgins inspired |
| ready |
higgins again |
| ready |
jQuery $.Callbacks |
| ready |
You can edit these tests or add more tests to this page by appending /edit to the URL.