Skip to content

Commit 75cbca8

Browse files
committed
perf: reduce size
1 parent 17b37c6 commit 75cbca8

2 files changed

Lines changed: 34 additions & 43 deletions

File tree

readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
- **Robust:** Works
3131
[cross-realm](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof#instanceof_and_multiple_realms)
3232
- **Secure:** Resilient to spoofing
33-
- **Tiny:** One 0.93 kB gzipped package instead of
33+
- **Tiny:** One 0.89 kB gzipped package instead of
3434
[`which-builtin-type`'s 10.5 kB gzipped total size](https://bundlejs.com/?q=which-builtin-type%401.2.1)
3535
with [50 dependencies](https://npmgraph.js.org/?q=which-builtin-type#zoom=w)
3636

src/index.ts

Lines changed: 33 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,7 @@ const builtinType = (value: unknown): BuiltinType => {
102102
}
103103
}
104104

105-
const INTERNAL_SLOT_STATIC_NAMES: [BuiltinType, string][] = [
106-
[`Array`, `isArray`],
107-
[`Buffer`, `isBuffer`],
108-
]
105+
const INTERNAL_SLOT_PREDICATE_NAMES: BuiltinType[] = [`Array`, `Buffer`]
109106
const INTERNAL_SLOT_PROTOTYPE_NAMES: [BuiltinType, string, any[]?][] = [
110107
[`Boolean`, `valueOf`],
111108
[`Number`, `valueOf`],
@@ -135,7 +132,7 @@ const INTERNAL_SLOT_PROTOTYPE_NAMES: [BuiltinType, string, any[]?][] = [
135132
[`Temporal.PlainMonthDay`, `monthCode`],
136133
[`Temporal.Duration`, `sign`],
137134
]
138-
const BUILTIN_ERROR_SUBCLASS_NAMES = new Set<string>([
135+
const BUILTIN_ERROR_SUBCLASS_NAMES = new Set<string | undefined>([
139136
`EvalError`,
140137
`RangeError`,
141138
`ReferenceError`,
@@ -145,18 +142,16 @@ const BUILTIN_ERROR_SUBCLASS_NAMES = new Set<string>([
145142
`AggregateError`,
146143
])
147144

148-
type BuiltinTypeFunction = (value: object) => BuiltinType | undefined
145+
type BuiltinTypeFunction = (value: object) => BuiltinType | `` | undefined
149146

150147
const TYPE_TAG_FUNCTIONS: BuiltinTypeFunction[] = [
151-
...INTERNAL_SLOT_STATIC_NAMES.flatMap<BuiltinTypeFunction>(([type, name]) => {
152-
const Class = globalThis[type as keyof typeof globalThis] as
153-
| Record<string, (value: unknown) => boolean>
154-
| undefined
155-
if (!Class) {
156-
return []
157-
}
158-
const func = Class[name]!
159-
return value => (func(value) ? type : undefined)
148+
...INTERNAL_SLOT_PREDICATE_NAMES.flatMap<BuiltinTypeFunction>(type => {
149+
const func = (
150+
globalThis[type as keyof typeof globalThis] as
151+
| Record<string, (value: unknown) => boolean>
152+
| undefined
153+
)?.[`is${type}`]
154+
return func ? value => (func(value) ? type : ``) : []
160155
}),
161156
...INTERNAL_SLOT_PROTOTYPE_NAMES.flatMap<BuiltinTypeFunction>(
162157
([type, name, args = []]) => {
@@ -173,13 +168,14 @@ const TYPE_TAG_FUNCTIONS: BuiltinTypeFunction[] = [
173168
return []
174169
}
175170
let descriptor: PropertyDescriptor | undefined
176-
do {
177-
descriptor = Object.getOwnPropertyDescriptor(prototype, name)
178-
if (descriptor) {
179-
break
180-
}
171+
for (
172+
;
173+
prototype &&
174+
!(descriptor = Object.getOwnPropertyDescriptor(prototype, name));
181175
prototype = Object.getPrototypeOf(prototype) as object | null
182-
} while (prototype)
176+
) {
177+
//
178+
}
183179
if (!descriptor) {
184180
return []
185181
}
@@ -192,9 +188,9 @@ const TYPE_TAG_FUNCTIONS: BuiltinTypeFunction[] = [
192188
// Each function above will do one of the following when applied to a
193189
// value not of the proper type:
194190
// 1. Throw
195-
// 2. Return `null` or `undefined
191+
// 2. Return `null` or `undefined`
196192
try {
197-
return func.apply(value, args) == null ? undefined : type
193+
return func.apply(value, args) == null ? `` : type
198194
} catch {
199195
return undefined
200196
}
@@ -203,7 +199,7 @@ const TYPE_TAG_FUNCTIONS: BuiltinTypeFunction[] = [
203199
),
204200
]
205201
const typedArrayToStringTag = Object.getOwnPropertyDescriptor(
206-
Object.getPrototypeOf(Uint8Array.prototype),
202+
Object.getPrototypeOf(Int8Array.prototype),
207203
Symbol.toStringTag,
208204
)?.get
209205
if (typedArrayToStringTag) {
@@ -212,26 +208,21 @@ if (typedArrayToStringTag) {
212208
)
213209
}
214210
TYPE_TAG_FUNCTIONS.push(value => {
215-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
216-
if (!(Error.isError?.(value) ?? toStringType(value) == `Error`)) {
217-
return undefined
211+
const type =
212+
!(Symbol.toStringTag in value) &&
213+
Object.prototype.toString.call(value).slice(8, -1)
214+
if (type == `Arguments`) {
215+
return type
218216
}
219-
const prototype = Object.getPrototypeOf(value) as { name?: string } | null
220-
if (prototype === null) {
221-
return `Error`
217+
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
218+
if (Error.isError?.(value) ?? type == `Error`) {
219+
const name = (Object.getPrototypeOf(value) as { name?: string } | null)
220+
?.name
221+
return BUILTIN_ERROR_SUBCLASS_NAMES.has(name)
222+
? (name as BuiltinType)
223+
: `Error`
222224
}
223-
return BUILTIN_ERROR_SUBCLASS_NAMES.has(prototype.name!)
224-
? (prototype.name as BuiltinType)
225-
: `Error`
225+
return undefined
226226
})
227-
TYPE_TAG_FUNCTIONS.push(value => {
228-
const type = toStringType(value)
229-
return type == `Arguments` ? type : undefined
230-
})
231-
232-
const toStringType = (value: object) =>
233-
Object.hasOwn(value, Symbol.toStringTag)
234-
? ``
235-
: Object.prototype.toString.call(value).slice(8, -1)
236227

237228
export default builtinType

0 commit comments

Comments
 (0)