Skip to content

Commit

Permalink
try to using maps?
Browse files Browse the repository at this point in the history
  • Loading branch information
debadree25 committed Feb 21, 2023
1 parent 4737a20 commit f6c2580
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 14 deletions.
2 changes: 1 addition & 1 deletion benchmark/events/eventtarget.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const common = require('../common.js');

const bench = common.createBenchmark(main, {
n: [1e6],
listeners: [1, 5, 10],
listeners: [1, 5, 10, 50, 100],
}, { flags: ['--expose-internals'] });

function main({ n, listeners }) {
Expand Down
55 changes: 42 additions & 13 deletions lib/internal/event_target.js
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,18 @@ function weakListeners() {
return { registry: weakListenersState, map: objectToWeakListenerMap };
}

let duplicateCheckerMap = null;
function duplicateChecker() {
duplicateCheckerMap ??= new SafeMap();
return duplicateCheckerMap;
}

let lastNodeMap = null;
function lastNode() {
lastNodeMap ??= new SafeMap();
return lastNodeMap;
}

const kFlagOnce = 1 << 0;
const kFlagCapture = 1 << 1;
const kFlagPassive = 1 << 2;
Expand All @@ -427,7 +439,7 @@ const kFlagRemoved = 1 << 5;
// slower.
class Listener {
constructor(previous, listener, once, capture, passive,
isNodeStyleListener, weak) {
isNodeStyleListener, weak, type) {
this.next = undefined;
if (previous !== undefined)
previous.next = this;
Expand Down Expand Up @@ -465,6 +477,16 @@ class Listener {
};
this.listener = listener;
}
if (duplicateChecker().has(type)) {
const listeners = duplicateChecker().get(type);
listeners.set(listener, capture);
} else {
const listeners = new SafeWeakMap();
listeners.set(listener, capture);
duplicateChecker().set(type, listeners);
}
lastNode().set(type, this);
this.isLastNode = true;
}

get once() {
Expand Down Expand Up @@ -505,6 +527,14 @@ class Listener {
this.removed = true;
if (this.weak)
weakListeners().registry.unregister(this);
if (this.isLastNode) {
lastNode().set(this.type, this.previous);
this.previous.isLastNode = true;
}
if (duplicateChecker().has(this.type)) {
const listeners = duplicateChecker().get(this.type);
listeners.delete(this.listener);
}
}
}

Expand Down Expand Up @@ -614,7 +644,7 @@ class EventTarget {
root = { size: 1, next: undefined };
// This is the first handler in our linked list.
new Listener(root, listener, once, capture, passive,
isNodeStyleListener, weak);
isNodeStyleListener, weak, type);
this[kNewListener](
root.size,
type,
Expand All @@ -627,21 +657,20 @@ class EventTarget {
return;
}

let handler = root.next;
let previous = root;

// We have to walk the linked list to see if we have a match
while (handler !== undefined && !handler.same(listener, capture)) {
previous = handler;
handler = handler.next;
if (duplicateChecker().has(type)) {
const typeListeners = duplicateChecker().get(type);
if (typeListeners.has(listener)) {
const listenerInfo = typeListeners.get(listener);
if (listenerInfo.capture === capture) {
return;
}
}
}

if (handler !== undefined) { // Duplicate! Ignore
return;
}
const previous = lastNode().get(type);

new Listener(previous, listener, once, capture, passive,
isNodeStyleListener, weak);
isNodeStyleListener, weak, type);
root.size++;
this[kNewListener](root.size, type, listener, once, capture, passive, weak);
}
Expand Down

0 comments on commit f6c2580

Please sign in to comment.