Skip to content

Commit 7a71cd7

Browse files
starkwangrefack
authored andcommitted
lib: move duplicate spliceOne into internal/util
lib/url.js and lib/events.js are using the same spliceOne function. This change is to move it into the internal/util for avoiding duplicate code. PR-URL: #16221 Reviewed-By: Timothy Gu <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Anatoli Papirovski <[email protected]> Reviewed-By: Refael Ackermann <[email protected]>
1 parent 686e092 commit 7a71cd7

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

lib/events.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
'use strict';
2323

2424
var domain;
25+
var spliceOne;
2526

2627
function EventEmitter() {
2728
EventEmitter.init.call(this);
@@ -414,8 +415,11 @@ EventEmitter.prototype.removeListener =
414415

415416
if (position === 0)
416417
list.shift();
417-
else
418+
else {
419+
if (spliceOne === undefined)
420+
spliceOne = require('internal/util').spliceOne;
418421
spliceOne(list, position);
422+
}
419423

420424
if (list.length === 1)
421425
events[type] = list[0];
@@ -527,13 +531,6 @@ EventEmitter.prototype.eventNames = function eventNames() {
527531
return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : [];
528532
};
529533

530-
// About 1.5x faster than the two-arg version of Array#splice().
531-
function spliceOne(list, index) {
532-
for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)
533-
list[i] = list[k];
534-
list.pop();
535-
}
536-
537534
function arrayClone(arr, n) {
538535
var copy = new Array(n);
539536
for (var i = 0; i < n; ++i)

lib/internal/util.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,13 @@ function join(output, separator) {
271271
return str;
272272
}
273273

274+
// About 1.5x faster than the two-arg version of Array#splice().
275+
function spliceOne(list, index) {
276+
for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)
277+
list[i] = list[k];
278+
list.pop();
279+
}
280+
274281
module.exports = {
275282
assertCrypto,
276283
cachedResult,
@@ -281,10 +288,11 @@ module.exports = {
281288
filterDuplicateStrings,
282289
getConstructorOf,
283290
isError,
291+
join,
284292
normalizeEncoding,
285293
objectToString,
286294
promisify,
287-
join,
295+
spliceOne,
288296

289297
// Symbol used to customize promisify conversion
290298
customPromisifyArgs: kCustomPromisifyArgsSymbol,

lib/url.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ const { hexTable } = require('internal/querystring');
2828

2929
const errors = require('internal/errors');
3030

31+
const { spliceOne } = require('internal/util');
32+
3133
// WHATWG URL implementation provided by internal/url
3234
const {
3335
URL,
@@ -950,13 +952,6 @@ Url.prototype.parseHost = function parseHost() {
950952
if (host) this.hostname = host;
951953
};
952954

953-
// About 1.5x faster than the two-arg version of Array#splice().
954-
function spliceOne(list, index) {
955-
for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)
956-
list[i] = list[k];
957-
list.pop();
958-
}
959-
960955
// These characters do not need escaping:
961956
// ! - . _ ~
962957
// ' ( ) * :

0 commit comments

Comments
 (0)