Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 /* | 1 /* |
2 * This file is part of Adblock Plus <https://adblockplus.org/>, | 2 * This file is part of Adblock Plus <https://adblockplus.org/>, |
3 * Copyright (C) 2006-present eyeo GmbH | 3 * Copyright (C) 2006-present eyeo GmbH |
4 * | 4 * |
5 * Adblock Plus is free software: you can redistribute it and/or modify | 5 * Adblock Plus is free software: you can redistribute it and/or modify |
6 * it under the terms of the GNU General Public License version 3 as | 6 * it under the terms of the GNU General Public License version 3 as |
7 * published by the Free Software Foundation. | 7 * published by the Free Software Foundation. |
8 * | 8 * |
9 * Adblock Plus is distributed in the hope that it will be useful, | 9 * Adblock Plus is distributed in the hope that it will be useful, |
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
(...skipping 17 matching lines...) Expand all Loading... | |
28 } | 28 } |
29 | 29 |
30 /** | 30 /** |
31 * Adds a listener for the specified event name. | 31 * Adds a listener for the specified event name. |
32 * | 32 * |
33 * @param {string} name | 33 * @param {string} name |
34 * @param {function} listener | 34 * @param {function} listener |
35 */ | 35 */ |
36 on(name, listener) | 36 on(name, listener) |
37 { | 37 { |
38 let listeners = this._listeners.size > 0 ? this._listeners.get(name) : null; | 38 let listeners = this._listeners.get(name); |
hub
2019/01/09 22:42:40
I don't see how this is faster (since it is part o
Manish Jethani
2019/01/09 23:09:45
This is not about null vs. undefined, this is abou
Manish Jethani
2019/01/09 23:11:55
Having said this, the "optimize all methods" part
Manish Jethani
2019/01/10 03:50:13
Alright, so I wrote this test:
// test.js
let
| |
39 if (listeners) | 39 if (listeners) |
40 listeners.push(listener); | 40 listeners.push(listener); |
41 else | 41 else |
42 this._listeners.set(name, [listener]); | 42 this._listeners.set(name, [listener]); |
43 } | 43 } |
44 | 44 |
45 /** | 45 /** |
46 * Removes a listener for the specified event name. | 46 * Removes a listener for the specified event name. |
47 * | 47 * |
48 * @param {string} name | 48 * @param {string} name |
49 * @param {function} listener | 49 * @param {function} listener |
50 */ | 50 */ |
51 off(name, listener) | 51 off(name, listener) |
52 { | 52 { |
53 let listeners = this._listeners.size > 0 ? this._listeners.get(name) : null; | 53 let listeners = this._listeners.get(name); |
54 if (listeners) | 54 if (listeners) |
55 { | 55 { |
56 if (listeners.length > 1) | 56 if (listeners.length > 1) |
57 { | 57 { |
58 let idx = listeners.indexOf(listener); | 58 let idx = listeners.indexOf(listener); |
59 if (idx != -1) | 59 if (idx != -1) |
60 listeners.splice(idx, 1); | 60 listeners.splice(idx, 1); |
61 } | 61 } |
62 else if (listeners[0] === listener) | 62 else if (listeners[0] === listener) |
63 { | 63 { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 let listeners = this._listeners.size > 0 ? this._listeners.get(name) : null; | 125 let listeners = this._listeners.size > 0 ? this._listeners.get(name) : null; |
126 if (listeners) | 126 if (listeners) |
127 { | 127 { |
128 for (let listener of listeners.slice()) | 128 for (let listener of listeners.slice()) |
129 listener(...args); | 129 listener(...args); |
130 } | 130 } |
131 } | 131 } |
132 } | 132 } |
133 | 133 |
134 exports.EventEmitter = EventEmitter; | 134 exports.EventEmitter = EventEmitter; |
LEFT | RIGHT |