diff options
author | Weijia Wang <381152119@qq.com> | 2017-10-15 23:59:53 +0800 |
---|---|---|
committer | Gibson Fahnestock <gibfahn@gmail.com> | 2017-10-31 00:15:13 +0000 |
commit | 6be494251b766c24be34c53d031204a1d0e0340b (patch) | |
tree | 4379e1444b06899c7cc82cf6135d1f5adc44a2fc | |
parent | 55ba1d41159dccbcb850073499bb21d845462c4e (diff) | |
download | node-new-6be494251b766c24be34c53d031204a1d0e0340b.tar.gz |
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: https://github.com/nodejs/node/pull/16221
Backport-PR-URL: https://github.com/nodejs/node/pull/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>
-rw-r--r-- | lib/events.js | 13 | ||||
-rw-r--r-- | lib/internal/util.js | 10 | ||||
-rw-r--r-- | lib/url.js | 8 |
3 files changed, 15 insertions, 16 deletions
diff --git a/lib/events.js b/lib/events.js index d615fc2132..0977f23445 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); @@ -389,8 +390,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]; @@ -502,13 +506,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 1a4e8d6247..a87bc68e76 100644 --- a/lib/internal/util.js +++ b/lib/internal/util.js @@ -266,6 +266,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, @@ -276,10 +283,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 052fb3cd46..2cc4488a3e 100644 --- a/lib/url.js +++ b/lib/url.js @@ -25,6 +25,7 @@ const { toASCII } = process.binding('config').hasIntl ? process.binding('icu') : require('punycode'); const { hexTable } = require('internal/querystring'); +const { spliceOne } = require('internal/util'); // WHATWG URL implementation provided by internal/url const { @@ -948,13 +949,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: // ! - . _ ~ // ' ( ) * : |