Skip to content

Commit

Permalink
fast dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
justinbmeyer authored and daffl committed Jun 10, 2014
1 parent 62d57db commit ed24af0
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 94 deletions.
10 changes: 7 additions & 3 deletions event/event.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,16 @@ steal('can/util/can.js', function (can) {

// Grab event listeners
var eventName = event.type,
handlers = (events[eventName] || []).slice(0);
handlers = (events[eventName] || []).slice(0),
passed = [event];

// Execute handlers listening for this event.
args = [event].concat(args || []);
if(args) {
passed.push.apply(passed, args);
}

for (var i = 0, len = handlers.length; i < len; i++) {
handlers[i].handler.apply(this, args);
handlers[i].handler.apply(this, passed);
}

return event;
Expand Down
14 changes: 9 additions & 5 deletions list/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,16 @@ <h2 id="qunit-userAgent"></h2>
document.write(unescape('%3Clink rel="stylesheet" type="text/css" href="../bower_components/qunit/qunit/qunit.css" /%3E'));
document.write(unescape('%3Cscript type="text/javascript" src="../bower_components/qunit/qunit/qunit.js" %3E%3C/script%3E'));
}

QUnit.config.autostart = false;
steal("can/list/list_test.js", function() {
can.dev.logLevel = 3;
QUnit.start();
});
setTimeout(function(){
steal("can/list/list_test.js", function() {
can.dev.logLevel = 3;
QUnit.start();
});

},300);


</script>
</body>
</html>
5 changes: 3 additions & 2 deletions map/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,8 @@ steal('can/util', 'can/util/bind','./bubble.js', 'can/construct', 'can/util/batc
// when a change happens, create the named event.
can.batch.trigger(this, {
type: attr,
batchNum: ev.batchNum
batchNum: ev.batchNum,
target: ev.target
}, [newVal, oldVal]);

if(how === "remove" || how === "add") {
Expand All @@ -308,7 +309,7 @@ steal('can/util', 'can/util/bind','./bubble.js', 'can/construct', 'can/util/batc
},
// Trigger a change event.
_triggerChange: function (attr, how, newVal, oldVal) {
can.batch.trigger(this, "change", can.makeArray(arguments));
can.batch.trigger(this, "change", arguments);
},
// Iterator that does not trigger live binding.
_each: function (callback) {
Expand Down
4 changes: 2 additions & 2 deletions model/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -1858,14 +1858,14 @@ steal('can/util', 'can/map', 'can/list', function (can) {
// handler( 'change','1.destroyed' ). This is used
// to remove items on destroyed from Model Lists.
// but there should be a better way.
can.trigger(this, "change", funcName);
can.dispatch.call(this, {type:"change", target: this}, [funcName]);

//!steal-remove-start
can.dev.log("Model.js - " + constructor.shortName + " " + funcName);
//!steal-remove-end

// Call event on the instance's Class
can.trigger(constructor, funcName, this);
can.dispatch.call(constructor, funcName, [this]);
};
});

Expand Down
11 changes: 7 additions & 4 deletions model/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@ <h2 id="qunit-userAgent"></h2>
}

QUnit.config.autostart = false;
steal("can/model/model_test.js", function() {
can.dev.logLevel = 3;
QUnit.start();
});
setTimeout(function(){
steal("can/model/model_test.js", function() {
can.dev.logLevel = 3;
QUnit.start();
});
},1000)

</script>
</body>
</html>
85 changes: 39 additions & 46 deletions test/jquery.html
Original file line number Diff line number Diff line change
Expand Up @@ -30,53 +30,46 @@ <h2 id="qunit-userAgent"></h2>

QUnit.config.autostart = false;

steal(function() {
var config = window.CONFIGURATION = {
"map": {
"*": {
"can/util/util.js": "can/util/jquery/jquery.js",
"jquery/jquery.js": "can/lib/jquery.1.10.2.js"
}
}
};
setTimeout(function(){

steal.config(config);
}, "can/component/component_test.js",
"can/construct/construct_test.js", "can/map/map_test.js",
"can/list/list_test.js",
"can/list/promise/promise_test.js",
"can/observe/observe_test.js",
"can/compute/compute_test.js", "can/model/model_test.js",
"can/view/view_test.js", "can/view/ejs/ejs_test.js",
"can/view/stache/stache_test.js",
"can/control/control_test.js", "can/route/route_test.js",
"can/control/route/route_test.js",
"can/view/mustache/mustache_test.js",
"can/route/pushstate/pushstate_test.js",
"can/model/queue/queue_test.js",
"can/construct/super/super_test.js",
"can/construct/proxy/proxy_test.js",
"can/map/lazy/lazy_test.js",
"can/map/delegate/delegate_test.js",
"can/map/setter/setter_test.js",
"can/map/attributes/attributes_test.js",
"can/map/validations/validations_test.js",
"can/map/backup/backup_test.js",
"can/map/list/list_test.js",
"can/map/define/define_test.js",
"can/map/sort/sort_test.js",
"can/control/plugin/plugin_test.js",
"can/view/modifiers/modifiers_test.js",
"can/util/object/object_test.js",
"can/util/fixture/fixture_test.js",
"can/view/bindings/bindings_test.js",
"can/view/live/live_test.js",
"can/view/scope/scope_test.js",
"can/util/string/string_test.js",
"can/util/attr/attr_test.js", function() {
can.dev.logLevel = 3;
QUnit.start();
});
steal( "can/component/component_test.js",
"can/construct/construct_test.js", "can/map/map_test.js",
"can/list/list_test.js",
"can/list/promise/promise_test.js",
"can/observe/observe_test.js",
"can/compute/compute_test.js", "can/model/model_test.js",
"can/view/view_test.js", "can/view/ejs/ejs_test.js",
"can/view/stache/stache_test.js",
"can/control/control_test.js", "can/route/route_test.js",
"can/control/route/route_test.js",
"can/view/mustache/mustache_test.js",
"can/route/pushstate/pushstate_test.js",
"can/model/queue/queue_test.js",
"can/construct/super/super_test.js",
"can/construct/proxy/proxy_test.js",
"can/map/lazy/lazy_test.js",
"can/map/delegate/delegate_test.js",
"can/map/setter/setter_test.js",
"can/map/attributes/attributes_test.js",
"can/map/validations/validations_test.js",
"can/map/backup/backup_test.js",
"can/map/list/list_test.js",
"can/map/define/define_test.js",
"can/map/sort/sort_test.js",
"can/control/plugin/plugin_test.js",
"can/view/modifiers/modifiers_test.js",
"can/util/object/object_test.js",
"can/util/fixture/fixture_test.js",
"can/view/bindings/bindings_test.js",
"can/view/live/live_test.js",
"can/view/scope/scope_test.js",
"can/util/string/string_test.js",
"can/util/attr/attr_test.js", function() {
can.dev.logLevel = 3;
QUnit.start();
});

},1000);
</script>
</body>

Expand Down
7 changes: 3 additions & 4 deletions util/batch/batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ steal('can/util/can.js', function (can) {
can.batch.start();
}
for(i = 0, len = items.length; i < len; i++) {
can.trigger.apply(can, items[i]);
can.dispatch.apply(items[i][0],items[i][1]);
}
for(i = 0, len = callbacks.length; i < callbacks.length; i++) {
callbacks[i]();
Expand All @@ -203,16 +203,15 @@ steal('can/util/can.js', function (can) {
// Don't send events if initalizing.
if (!item._init) {
if (transactions === 0) {
return can.trigger(item, event, args);
return can.dispatch.call(item, event, args);
} else {
event = typeof event === 'string' ? {
type: event
} : event;
event.batchNum = batchNum;
batchEvents.push([
item,
event,
args
[event, args]
]);
}
}
Expand Down
10 changes: 10 additions & 0 deletions util/jquery/jquery.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ steal('jquery', 'can/util/can.js', 'can/util/attr', "can/event", 'can/util/array
};
}
event.target = event.target || obj;
if(args){
if( args.length && typeof args === "string") {
args = [args];
} else if(! args.length ) {
args = [args];
}
}
if(!args){
args = [];
}
can.dispatch.call(obj, event, args);
}
},
Expand Down
70 changes: 42 additions & 28 deletions view/stache/visual_benchmark.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
<body>
<button id='start'>Start</button>
<button id='stop'>Stop</button>
<div id='timing'></div>
<script type="text/javascript" src="../../lib/steal/steal.js"></script>
<script type="text/javascript">

Expand All @@ -65,19 +66,19 @@
count: 0,
content: 0,
tick: function(){
var count = this.attr("count")+1
this.attr({
count: count,
left: Math.cos(count / 10) * 10,
top: Math.sin(count / 10) * 10,
color: count % 255,
content: count
})
var count = this.attr('count') + 1;
this.attr('count', count);
this.attr('top', Math.sin(count / 10) * 10);
this.attr('left', Math.cos(count / 10) * 10);
this.attr('color', count % 255);
this.attr('content', count % 100);
}
});

for(var i =0; i < 100; i++) {
boxes.push( new Box({ number: i }) );
var box = new Box({ number: i });
box.tick();
boxes.push( box );
}

var frag = template({boxes: boxes});
Expand All @@ -88,29 +89,42 @@
var count = 0;


var run = function(){
can.batch.start();
for(var n = 0 ; n < boxes.length; n++) {
boxes[n].tick();
}
can.batch.stop();
};

$("#start").click(function(){
var start = new Date();
setTimeout(function(){
count++
for(var n = 0 ; n < boxes.length; n++) {
boxes[n].tick();
}

if(count < 100) {
setTimeout(arguments.callee, 0);
} else {
console.log("100 spinning boxes spinning 100 times", new Date - start)
count = 0;
}

},0)
})
loopCount = 0;
totalTime = 0;
benchmarkLoop(run);
});




window.timeout = null;
window.totalTime = null;
window.loopCount = null;
window.benchmarkLoop = function(fn) {
var startDate = new Date();
fn();
var endDate = new Date();
totalTime += endDate - startDate;
loopCount++;
if (loopCount % 20 === 0) {
$('#timing').text('Performed ' + loopCount + ' iterations in ' + totalTime + ' ms (average ' + (totalTime / loopCount).toFixed(2) + ' ms per loop).');
}
if(loopCount < 10) {
timeout = setTimeout(function(){
benchmarkLoop(fn);
},1);
}

};

})
});


</script>
Expand Down

0 comments on commit ed24af0

Please sign in to comment.