Skip to content

Commit 6be4942

Browse files
starkwanggibfahn
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 Backport-PR-URL: #16433 Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Vse Mozhet Byt <vsemozhetbyt@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Anatoli Papirovski <apapirovski@mac.com> Reviewed-By: Refael Ackermann <refack@gmail.com>
1 parent 55ba1d4 commit 6be4942

File tree

3 files changed

+15
-16
lines changed

3 files changed

+15
-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);
@@ -389,8 +390,11 @@ EventEmitter.prototype.removeListener =
389390

390391
if (position === 0)
391392
list.shift();
392-
else
393+
else {
394+
if (spliceOne === undefined)
395+
spliceOne = require('internal/util').spliceOne;
393396
spliceOne(list, position);
397+
}
394398

395399
if (list.length === 1)
396400
events[type] = list[0];
@@ -502,13 +506,6 @@ EventEmitter.prototype.eventNames = function eventNames() {
502506
return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : [];
503507
};
504508

505-
// About 1.5x faster than the two-arg version of Array#splice().
506-
function spliceOne(list, index) {
507-
for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)
508-
list[i] = list[k];
509-
list.pop();
510-
}
511-
512509
function arrayClone(arr, n) {
513510
var copy = new Array(n);
514511
for (var i = 0; i < n; ++i)

lib/internal/util.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,13 @@ function join(output, separator) {
266266
return str;
267267
}
268268

269+
// About 1.5x faster than the two-arg version of Array#splice().
270+
function spliceOne(list, index) {
271+
for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1)
272+
list[i] = list[k];
273+
list.pop();
274+
}
275+
269276
module.exports = {
270277
assertCrypto,
271278
cachedResult,
@@ -276,10 +283,11 @@ module.exports = {
276283
filterDuplicateStrings,
277284
getConstructorOf,
278285
isError,
286+
join,
279287
normalizeEncoding,
280288
objectToString,
281289
promisify,
282-
join,
290+
spliceOne,
283291

284292
// Symbol used to customize promisify conversion
285293
customPromisifyArgs: kCustomPromisifyArgsSymbol,

lib/url.js

+1-7
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const { toASCII } = process.binding('config').hasIntl ?
2525
process.binding('icu') : require('punycode');
2626

2727
const { hexTable } = require('internal/querystring');
28+
const { spliceOne } = require('internal/util');
2829

2930
// WHATWG URL implementation provided by internal/url
3031
const {
@@ -948,13 +949,6 @@ Url.prototype.parseHost = function parseHost() {
948949
if (host) this.hostname = host;
949950
};
950951

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

0 commit comments

Comments
 (0)