Skip to content

Commit 22f51a6

Browse files
cjihrigtargos
authored andcommittedOct 3, 2018
cluster: use Map to track callbacks
Use a Map to avoid delete operations in callback tracking. PR-URL: #23125 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]> Reviewed-By: Denys Otrishko <[email protected]>
1 parent 3101096 commit 22f51a6

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed
 

‎lib/internal/cluster/utils.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ module.exports = {
77
handles: {} // Used in tests.
88
};
99

10-
const callbacks = {};
10+
const callbacks = new Map();
1111
var seq = 0;
1212

1313
function sendHelper(proc, message, handle, cb) {
@@ -18,7 +18,7 @@ function sendHelper(proc, message, handle, cb) {
1818
message = util._extend({ cmd: 'NODE_CLUSTER' }, message);
1919

2020
if (typeof cb === 'function')
21-
callbacks[seq] = cb;
21+
callbacks.set(seq, cb);
2222

2323
message.seq = seq;
2424
seq += 1;
@@ -34,9 +34,13 @@ function internal(worker, cb) {
3434

3535
var fn = cb;
3636

37-
if (message.ack !== undefined && callbacks[message.ack] !== undefined) {
38-
fn = callbacks[message.ack];
39-
delete callbacks[message.ack];
37+
if (message.ack !== undefined) {
38+
const callback = callbacks.get(message.ack);
39+
40+
if (callback !== undefined) {
41+
fn = callback;
42+
callbacks.delete(message.ack);
43+
}
4044
}
4145

4246
fn.apply(worker, arguments);

0 commit comments

Comments
 (0)
Please sign in to comment.