Skip to content

Commit 380389a

Browse files
committed
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.
1 parent 4247913 commit 380389a

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

lib/events.js

Lines changed: 5 additions & 8 deletions
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);
@@ -416,8 +417,11 @@ EventEmitter.prototype.removeListener =
416417

417418
if (position === 0)
418419
list.shift();
419-
else
420+
else {
421+
if (spliceOne === undefined)
422+
spliceOne = require('internal/util').spliceOne;
420423
spliceOne(list, position);
424+
}
421425

422426
if (list.length === 1)
423427
events[type] = list[0];
@@ -529,13 +533,6 @@ EventEmitter.prototype.eventNames = function eventNames() {
529533
return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : [];
530534
};
531535

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

lib/internal/util.js

Lines changed: 9 additions & 1 deletion
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

Lines changed: 2 additions & 7 deletions
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,
@@ -952,13 +954,6 @@ Url.prototype.parseHost = function parseHost() {
952954
if (host) this.hostname = host;
953955
};
954956

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

0 commit comments

Comments
 (0)