Skip to content

Commit 0e6dca9

Browse files
committed
Optimize arguments usage
Fixes #263
1 parent a86040b commit 0e6dca9

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

lib/event/emitter.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@ EventEmitter.prototype.once = function(type, listener) {
3434
this.on(type, g);
3535
};
3636

37-
EventEmitter.prototype.emit = function(type) {
37+
EventEmitter.prototype.emit = function() {
38+
var type = arguments[0];
3839
var listeners = this._listeners[type];
3940
if (!listeners) {
4041
return;
4142
}
42-
var args = Array.prototype.slice.call(arguments, 1);
43+
// equivalent of Array.prototype.slice.call(arguments, 1);
44+
var l = arguments.length;
45+
var args = new Array(l - 1);
46+
for (var ai = 1; ai < l; ai++) {
47+
args[ai - 1] = arguments[ai];
48+
}
4349
for (var i = 0; i < listeners.length; i++) {
4450
listeners[i].apply(this, args);
4551
}

lib/event/eventtarget.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ EventTarget.prototype.removeEventListener = function(eventType, listener) {
3838
}
3939
};
4040

41-
EventTarget.prototype.dispatchEvent = function(event) {
41+
EventTarget.prototype.dispatchEvent = function() {
42+
var event = arguments[0];
4243
var t = event.type;
43-
var args = Array.prototype.slice.call(arguments, 0);
44+
// equivalent of Array.prototype.slice.call(arguments, 0);
45+
var args = arguments.length === 1 ? [event] : Array.apply(null, arguments);
4446
// TODO: This doesn't match the real behavior; per spec, onfoo get
4547
// their place in line from the /first/ time they're set from
4648
// non-null. Although WebKit bumps it to the end every time it's

0 commit comments

Comments
 (0)