-
Notifications
You must be signed in to change notification settings - Fork 30.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
Showing
18 changed files
with
141 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
'use strict'; | ||
|
||
const ReflectApply = Reflect.apply; | ||
|
||
// This function is borrowed from the function with the same name on V8 Extras' | ||
// `utils` object. V8 implements Reflect.apply very efficiently in conjunction | ||
// with the spread syntax, such that no additional special case is needed for | ||
// function calls w/o arguments. | ||
// Refs: https://github.com/v8/v8/blob/d6ead37d265d7215cf9c5f768f279e21bd170212/src/js/prologue.js#L152-L156 | ||
function uncurryThis(func) { | ||
return (thisArg, ...args) => ReflectApply(func, thisArg, args); | ||
} | ||
|
||
const TypedArrayPrototype = Object.getPrototypeOf(Uint8Array.prototype); | ||
|
||
const TypedArrayProto_toStringTag = | ||
uncurryThis( | ||
Object.getOwnPropertyDescriptor(TypedArrayPrototype, | ||
Symbol.toStringTag).get); | ||
|
||
// Cached to make sure no userland code can tamper with it. | ||
const isArrayBufferView = ArrayBuffer.isView; | ||
|
||
function isTypedArray(value) { | ||
return TypedArrayProto_toStringTag(value) !== undefined; | ||
} | ||
|
||
function isUint8Array(value) { | ||
return TypedArrayProto_toStringTag(value) === 'Uint8Array'; | ||
} | ||
|
||
module.exports = { | ||
isArrayBufferView, | ||
isTypedArray, | ||
isUint8Array | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
'use strict'; | ||
|
||
// Flags: --expose-internals | ||
|
||
require('../common'); | ||
const assert = require('assert'); | ||
const types = require('internal/util/types'); | ||
|
||
const primitive = true; | ||
const arrayBuffer = new ArrayBuffer(); | ||
const dataView = new DataView(arrayBuffer); | ||
const int32Array = new Int32Array(arrayBuffer); | ||
const uint8Array = new Uint8Array(arrayBuffer); | ||
const buffer = Buffer.from(arrayBuffer); | ||
|
||
const fakeDataView = Object.create(DataView.prototype); | ||
const fakeInt32Array = Object.create(Int32Array.prototype); | ||
const fakeUint8Array = Object.create(Uint8Array.prototype); | ||
const fakeBuffer = Object.create(Buffer.prototype); | ||
|
||
const stealthyDataView = | ||
Object.setPrototypeOf(new DataView(arrayBuffer), Uint8Array.prototype); | ||
const stealthyInt32Array = | ||
Object.setPrototypeOf(new Int32Array(arrayBuffer), uint8Array); | ||
const stealthyUint8Array = | ||
Object.setPrototypeOf(new Uint8Array(arrayBuffer), ArrayBuffer.prototype); | ||
|
||
const all = [ | ||
primitive, arrayBuffer, dataView, int32Array, uint8Array, buffer, | ||
fakeDataView, fakeInt32Array, fakeUint8Array, fakeBuffer, | ||
stealthyDataView, stealthyInt32Array, stealthyUint8Array | ||
]; | ||
|
||
const expected = { | ||
isArrayBufferView: [ | ||
dataView, int32Array, uint8Array, buffer, | ||
stealthyDataView, stealthyInt32Array, stealthyUint8Array | ||
], | ||
isTypedArray: [ | ||
int32Array, uint8Array, buffer, stealthyInt32Array, stealthyUint8Array | ||
], | ||
isUint8Array: [ | ||
uint8Array, buffer, stealthyUint8Array | ||
] | ||
}; | ||
|
||
for (const testedFunc of Object.keys(expected)) { | ||
const func = types[testedFunc]; | ||
const yup = []; | ||
for (const value of all) { | ||
if (func(value)) { | ||
yup.push(value); | ||
} | ||
} | ||
console.log('Testing', testedFunc); | ||
assert.deepStrictEqual(yup, expected[testedFunc]); | ||
} |