feat(lib): switch to typeof for better performance

This commit is contained in:
PalmDevs 2025-07-22 19:00:20 +07:00
parent b7ed6bb88f
commit 3c6aa155ca
No known key found for this signature in database
GPG key ID: 6FAE8F8677ACA8BA
5 changed files with 24 additions and 21 deletions

View file

@ -85,7 +85,7 @@ export type ByProps = FilterGenerator<
>
/**
* Filter modules by their exports having all of the specified properties with truthy values.
* Filter modules by their exports having all of the specified properties.
*
* @param prop The property to check for.
* @param props More properties to check for (optional).
@ -98,7 +98,8 @@ export type ByProps = FilterGenerator<
*/
export const byProps = createFilterGenerator<Parameters<ByProps>>(
(props, _, exports) => {
if (exports instanceof Object) {
const type = typeof exports
if (type === 'object' || type === 'function') {
for (const prop of props) {
if (prop in exports) continue
return false
@ -127,7 +128,8 @@ export type WithoutProps = FilterGenerator<
*/
export const withoutProps = createFilterGenerator<Parameters<WithoutProps>>(
(props, _, exports) => {
if (exports instanceof Object)
const type = typeof exports
if (type === 'object' || type === 'function')
for (const prop of props) if (prop in exports) return false
return true
@ -152,7 +154,7 @@ export type BySingleProp = FilterGenerator<
*/
export const bySingleProp = createFilterGenerator<Parameters<BySingleProp>>(
([prop], _, exports) => {
if (exports instanceof Object && prop in exports) {
if (typeof exports === 'object' && prop in exports) {
let hasMultipleProperties = false
for (const _ in exports) {
if (hasMultipleProperties) return false

View file

@ -116,22 +116,23 @@ function handleFactoryCall(
// Blacklist exports that:
// - are primitives (https://developer.mozilla.org/en-US/docs/Glossary/Primitive)
// - are empty objects
if (exports instanceof Object)
switch (exports.__proto__) {
case Object.prototype:
// This null case is for nativeModuleProxy specifically
// @ts-expect-error: Intentional
// biome-ignore lint/suspicious/noFallthroughSwitchClause: Intentional
case null:
if (!Reflect.ownKeys(exports).length) {
cacheBlacklistedModule(mInitializingId)
break
}
switch (typeof exports) {
case 'function':
mInitialized.add(mInitializingId)
break
default:
// @ts-expect-error: Intentional
// biome-ignore lint/suspicious/noFallthroughSwitchClause: Intentional
case 'object': {
if (Reflect.ownKeys(exports).length) {
mInitialized.add(mInitializingId)
break
}
}
else cacheBlacklistedModule(mInitializingId)
default:
cacheBlacklistedModule(mInitializingId)
}
}
executeInitializeSubscriptions(mInitializingId, exports)

View file

@ -82,8 +82,8 @@ export const byGeneratedIconComponent = createFilterGenerator<
Parameters<ByGeneratedIconComponent>
>(
(names, id, exports) => {
if (exports instanceof Object) {
if (exports[names[0]] instanceof Function) {
if (typeof exports === 'object') {
if (typeof exports[names[0]] === 'function') {
let hasMultipleProps = false
for (const _ in exports) {
if (hasMultipleProps) return false

View file

@ -8,7 +8,7 @@ export { objectFreeze } from './patches/object'
* @param val The value to check.
*/
export function isObject(val: any): val is AnyObject {
return val?.__proto__ === Object.prototype
return typeof val === 'object' && val !== null && !Array.isArray(val)
}
/**

View file

@ -61,7 +61,7 @@ const _handler = {
const target = unproxifyFromHint(hint)
const val = Reflect.get(target!, p, recv)
if (_metas.get(hint)?.bind && val instanceof Function)
if (_metas.get(hint)?.bind && typeof val === 'function')
return new Proxy(val, {
// If thisArg happens to be a proxified value, we will use the target object instead
apply: (fn, thisArg, args) =>