Skip to content

Commit 67aed79

Browse files
TimothyGuevanlucas
authored andcommitted
lib: faster type checks for some types
Backport-PR-URL: #16073 PR-URL: #15663 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: Benedikt Meurer <benedikt.meurer@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent b431a49 commit 67aed79

File tree

18 files changed

+141
-24
lines changed

18 files changed

+141
-24
lines changed

lib/assert.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
const { compare } = process.binding('buffer');
2424
const { isSet, isMap, isDate, isRegExp } = process.binding('util');
2525
const { objectToString } = require('internal/util');
26+
const { isArrayBufferView } = require('internal/util/types');
2627
const errors = require('internal/errors');
2728

2829
// The assert module provides functions that throw
@@ -198,7 +199,7 @@ function strictDeepEqual(actual, expected) {
198199
if (actual.message !== expected.message) {
199200
return false;
200201
}
201-
} else if (!isFloatTypedArrayTag(actualTag) && ArrayBuffer.isView(actual)) {
202+
} else if (!isFloatTypedArrayTag(actualTag) && isArrayBufferView(actual)) {
202203
if (!areSimilarTypedArrays(actual, expected)) {
203204
return false;
204205
}
@@ -254,7 +255,7 @@ function looseDeepEqual(actual, expected) {
254255
const expectedTag = objectToString(expected);
255256
if (actualTag === expectedTag) {
256257
if (!isObjectOrArrayTag(actualTag) && !isFloatTypedArrayTag(actualTag) &&
257-
ArrayBuffer.isView(actual)) {
258+
isArrayBufferView(actual)) {
258259
return areSimilarTypedArrays(actual, expected);
259260
}
260261
// Ensure reflexivity of deepEqual with `arguments` objects.

lib/buffer.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,11 @@
2424
const binding = process.binding('buffer');
2525
const config = process.binding('config');
2626
const { compare: compare_, compareOffset } = binding;
27-
const { isAnyArrayBuffer, isUint8Array } = process.binding('util');
27+
const { isAnyArrayBuffer } = process.binding('util');
28+
const {
29+
isArrayBufferView,
30+
isUint8Array
31+
} = require('internal/util/types');
2832
const bindingObj = {};
2933
const internalUtil = require('internal/util');
3034
const pendingDeprecation = !!config.pendingDeprecation;
@@ -468,7 +472,7 @@ function base64ByteLength(str, bytes) {
468472

469473
function byteLength(string, encoding) {
470474
if (typeof string !== 'string') {
471-
if (ArrayBuffer.isView(string) || isAnyArrayBuffer(string)) {
475+
if (isArrayBufferView(string) || isAnyArrayBuffer(string)) {
472476
return string.byteLength;
473477
}
474478

lib/child_process.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
const util = require('util');
2525
const { deprecate, convertToValidSignal } = require('internal/util');
26+
const { isUint8Array } = require('internal/util/types');
2627
const { createPromise,
2728
promiseResolve, promiseReject } = process.binding('util');
2829
const debug = util.debuglog('child_process');
@@ -31,7 +32,6 @@ const uv = process.binding('uv');
3132
const spawn_sync = process.binding('spawn_sync');
3233
const Buffer = require('buffer').Buffer;
3334
const Pipe = process.binding('pipe_wrap').Pipe;
34-
const { isUint8Array } = process.binding('util');
3535
const child_process = require('internal/child_process');
3636

3737
const errnoException = util._errnoException;

lib/crypto.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,10 @@ const Buffer = require('buffer').Buffer;
4343
const kBufferMaxLength = require('buffer').kMaxLength;
4444
const stream = require('stream');
4545
const util = require('util');
46-
const { isUint8Array } = process.binding('util');
46+
const {
47+
isArrayBufferView,
48+
isUint8Array
49+
} = require('internal/util/types');
4750
const LazyTransform = require('internal/streams/lazy_transform');
4851

4952
const DH_GENERATOR = 2;
@@ -416,7 +419,7 @@ function DiffieHellman(sizeOrKey, keyEncoding, generator, genEncoding) {
416419

417420
if (typeof sizeOrKey !== 'number' &&
418421
typeof sizeOrKey !== 'string' &&
419-
!ArrayBuffer.isView(sizeOrKey)) {
422+
!isArrayBufferView(sizeOrKey)) {
420423
throw new TypeError('First argument should be number, string, ' +
421424
'Buffer, TypedArray, or DataView');
422425
}

lib/dgram.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const errors = require('internal/errors');
2626
const Buffer = require('buffer').Buffer;
2727
const dns = require('dns');
2828
const util = require('util');
29+
const { isUint8Array } = require('internal/util/types');
2930
const EventEmitter = require('events');
3031
const setInitTriggerId = require('async_hooks').setInitTriggerId;
3132
const UV_UDP_REUSEADDR = process.binding('constants').os.UV_UDP_REUSEADDR;
@@ -34,7 +35,6 @@ const nextTick = require('internal/process/next_tick').nextTick;
3435

3536
const UDP = process.binding('udp_wrap').UDP;
3637
const SendWrap = process.binding('udp_wrap').SendWrap;
37-
const { isUint8Array } = process.binding('util');
3838

3939
const BIND_STATE_UNBOUND = 0;
4040
const BIND_STATE_BINDING = 1;

lib/fs.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ const constants = process.binding('constants').fs;
2828
const { S_IFIFO, S_IFLNK, S_IFMT, S_IFREG, S_IFSOCK } = constants;
2929
const util = require('util');
3030
const pathModule = require('path');
31-
const { isUint8Array, createPromise, promiseResolve } = process.binding('util');
31+
const { isUint8Array } = require('internal/util/types');
32+
const { createPromise, promiseResolve } = process.binding('util');
3233

3334
const binding = process.binding('fs');
3435
const fs = exports;

lib/internal/buffer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const normalizeEncoding = require('internal/util').normalizeEncoding;
88
const Buffer = require('buffer').Buffer;
99

1010
const icu = process.binding('icu');
11-
const { isUint8Array } = process.binding('util');
11+
const { isUint8Array } = require('internal/util/types');
1212

1313
// Transcodes the Buffer from one encoding to another, returning a new
1414
// Buffer instance.

lib/internal/child_process.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ const TTY = process.binding('tty_wrap').TTY;
1616
const TCP = process.binding('tcp_wrap').TCP;
1717
const UDP = process.binding('udp_wrap').UDP;
1818
const SocketList = require('internal/socket_list');
19-
const { isUint8Array } = process.binding('util');
2019
const { convertToValidSignal } = require('internal/util');
20+
const { isUint8Array } = require('internal/util/types');
2121

2222
const errnoException = util._errnoException;
2323
const SocketListSend = SocketList.SocketListSend;

lib/internal/encoding.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const {
2020
customInspectSymbol: inspect
2121
} = require('internal/util');
2222

23+
const { isArrayBufferView } = require('internal/util/types');
24+
2325
const {
2426
isArrayBuffer
2527
} = process.binding('util');
@@ -386,7 +388,7 @@ function makeTextDecoderICU() {
386388
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
387389
if (isArrayBuffer(input)) {
388390
input = lazyBuffer().from(input);
389-
} else if (!ArrayBuffer.isView(input)) {
391+
} else if (!isArrayBufferView(input)) {
390392
throw new errors.TypeError('ERR_INVALID_ARG_TYPE', 'input',
391393
['ArrayBuffer', 'ArrayBufferView']);
392394
}
@@ -462,7 +464,7 @@ function makeTextDecoderJS() {
462464
throw new errors.TypeError('ERR_INVALID_THIS', 'TextDecoder');
463465
if (isArrayBuffer(input)) {
464466
input = lazyBuffer().from(input);
465-
} else if (ArrayBuffer.isView(input)) {
467+
} else if (isArrayBufferView(input)) {
466468
input = lazyBuffer().from(input.buffer, input.byteOffset,
467469
input.byteLength);
468470
} else {

lib/internal/http2/core.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ const { onServerStream,
2121
} = require('internal/http2/compat');
2222
const { utcDate } = require('internal/http');
2323
const { promisify } = require('internal/util');
24+
const { isUint8Array } = require('internal/util/types');
2425
const { _connectionListener: httpConnectionListener } = require('http');
25-
const { isUint8Array, createPromise, promiseResolve } = process.binding('util');
26+
const { createPromise, promiseResolve } = process.binding('util');
2627
const debug = util.debuglog('http2');
2728

2829

0 commit comments

Comments
 (0)