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
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>
This commit is contained in:
Weijia Wang 2017-10-15 23:59:53 +08:00 committed by Refael Ackermann
parent 686e092202
commit 7a71cd7d0c
No known key found for this signature in database
GPG Key ID: CD704BD80FDDDB64
3 changed files with 16 additions and 16 deletions

View File

@ -22,6 +22,7 @@
'use strict'; 'use strict';
var domain; var domain;
var spliceOne;
function EventEmitter() { function EventEmitter() {
EventEmitter.init.call(this); EventEmitter.init.call(this);
@ -414,8 +415,11 @@ EventEmitter.prototype.removeListener =
if (position === 0) if (position === 0)
list.shift(); list.shift();
else else {
if (spliceOne === undefined)
spliceOne = require('internal/util').spliceOne;
spliceOne(list, position); spliceOne(list, position);
}
if (list.length === 1) if (list.length === 1)
events[type] = list[0]; events[type] = list[0];
@ -527,13 +531,6 @@ EventEmitter.prototype.eventNames = function eventNames() {
return this._eventsCount > 0 ? Reflect.ownKeys(this._events) : []; 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) { function arrayClone(arr, n) {
var copy = new Array(n); var copy = new Array(n);
for (var i = 0; i < n; ++i) for (var i = 0; i < n; ++i)

View File

@ -271,6 +271,13 @@ function join(output, separator) {
return str; 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 = { module.exports = {
assertCrypto, assertCrypto,
cachedResult, cachedResult,
@ -281,10 +288,11 @@ module.exports = {
filterDuplicateStrings, filterDuplicateStrings,
getConstructorOf, getConstructorOf,
isError, isError,
join,
normalizeEncoding, normalizeEncoding,
objectToString, objectToString,
promisify, promisify,
join, spliceOne,
// Symbol used to customize promisify conversion // Symbol used to customize promisify conversion
customPromisifyArgs: kCustomPromisifyArgsSymbol, customPromisifyArgs: kCustomPromisifyArgsSymbol,

View File

@ -28,6 +28,8 @@ const { hexTable } = require('internal/querystring');
const errors = require('internal/errors'); const errors = require('internal/errors');
const { spliceOne } = require('internal/util');
// WHATWG URL implementation provided by internal/url // WHATWG URL implementation provided by internal/url
const { const {
URL, URL,
@ -950,13 +952,6 @@ Url.prototype.parseHost = function parseHost() {
if (host) this.hostname = host; 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: // These characters do not need escaping:
// ! - . _ ~ // ! - . _ ~
// ' ( ) * : // ' ( ) * :