From 380389a3b1979af4ac5833047be1a415d7eececb Mon Sep 17 00:00:00 2001 From: Weijia Wang <381152119@qq.com> Date: Sun, 15 Oct 2017 23:59:53 +0800 Subject: [PATCH] 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. --- lib/events.js | 13 +++++-------- lib/internal/util.js | 10 +++++++++- lib/url.js | 9 ++------- 3 files changed, 16 insertions(+), 16 deletions(-) diff --git a/lib/events.js b/lib/events.js index 1414a1429dfe3b..e9b279b676a42b 100644 --- a/lib/events.js +++ b/lib/events.js @@ -22,6 +22,7 @@ 'use strict'; var domain; +var spliceOne; function EventEmitter() { EventEmitter.init.call(this); @@ -416,8 +417,11 @@ EventEmitter.prototype.removeListener = if (position === 0) list.shift(); - else + else { + if (spliceOne === undefined) + spliceOne = require('internal/util').spliceOne; spliceOne(list, position); + } if (list.length === 1) events[type] = list[0]; @@ -529,13 +533,6 @@ EventEmitter.prototype.eventNames = function eventNames() { return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : []; }; -// About 1.5x faster than the two-arg version of Array#splice(). -function spliceOne(list, index) { - for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) - list[i] = list[k]; - list.pop(); -} - function arrayClone(arr, n) { var copy = new Array(n); for (var i = 0; i < n; ++i) diff --git a/lib/internal/util.js b/lib/internal/util.js index 3bae5b7f398921..8eed15a771ff10 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -271,6 +271,13 @@ function join(output, separator) { return str; } +// About 1.5x faster than the two-arg version of Array#splice(). +function spliceOne(list, index) { + for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) + list[i] = list[k]; + list.pop(); +} + module.exports = { assertCrypto, cachedResult, @@ -281,10 +288,11 @@ module.exports = { filterDuplicateStrings, getConstructorOf, isError, + join, normalizeEncoding, objectToString, promisify, - join, + spliceOne, // Symbol used to customize promisify conversion customPromisifyArgs: kCustomPromisifyArgsSymbol, diff --git a/lib/url.js b/lib/url.js index b1e0ff8da56014..65a9585a02d220 100644 --- a/lib/url.js +++ b/lib/url.js @@ -28,6 +28,8 @@ const { hexTable } = require('internal/querystring'); const errors = require('internal/errors'); +const { spliceOne } = require('internal/util'); + // WHATWG URL implementation provided by internal/url const { URL, @@ -952,13 +954,6 @@ Url.prototype.parseHost = function parseHost() { if (host) this.hostname = host; }; -// About 1.5x faster than the two-arg version of Array#splice(). -function spliceOne(list, index) { - for (var i = index, k = i + 1, n = list.length; k < n; i += 1, k += 1) - list[i] = list[k]; - list.pop(); -} - // These characters do not need escaping: // ! - . _ ~ // ' ( ) * :