Skip to content

Commit

Permalink
Micro-optimize Object.getPrototypeOf uses
Browse files Browse the repository at this point in the history
  • Loading branch information
markerikson committed Apr 10, 2023
1 parent 3ad1df5 commit d897c6b
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
7 changes: 4 additions & 3 deletions src/core/proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
AnyArray,
Objectish,
getCurrentScope,
getPrototypeOf,
DRAFT_STATE,
die,
createProxy,
Expand Down Expand Up @@ -200,7 +201,7 @@ export const objectTraps: ProxyHandler<ProxyState> = {
die(11)
},
getPrototypeOf(state) {
return Object.getPrototypeOf(state.base_)
return getPrototypeOf(state.base_)
},
setPrototypeOf() {
die(12)
Expand Down Expand Up @@ -259,11 +260,11 @@ function getDescriptorFromProto(
): PropertyDescriptor | undefined {
// 'in' checks proto!
if (!(prop in source)) return undefined
let proto = Object.getPrototypeOf(source)
let proto = getPrototypeOf(source)
while (proto) {
const desc = Object.getOwnPropertyDescriptor(proto, prop)
if (desc) return desc
proto = Object.getPrototypeOf(proto)
proto = getPrototypeOf(proto)
}
return undefined
}
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/patches.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
each,
has,
getArchtype,
getPrototypeOf,
isSet,
isMap,
loadPlugin,
Expand Down Expand Up @@ -296,7 +297,7 @@ export function enablePatches() {
Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])
)
if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))
const cloned = Object.create(Object.getPrototypeOf(obj))
const cloned = Object.create(getPrototypeOf(obj))
for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])
if (has(obj, immerable)) cloned[immerable] = obj[immerable]
return cloned
Expand Down
8 changes: 5 additions & 3 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import {
die
} from "../internal"

export const getPrototypeOf = Object.getPrototypeOf

/** Returns true if the given value is an Immer draft */
/*#__PURE__*/
export function isDraft(value: any): boolean {
Expand All @@ -35,7 +37,7 @@ const objectCtorString = Object.prototype.constructor.toString()
/*#__PURE__*/
export function isPlainObject(value: any): boolean {
if (!value || typeof value !== "object") return false
const proto = Object.getPrototypeOf(value)
const proto = getPrototypeOf(value)
if (proto === null) {
return true
}
Expand Down Expand Up @@ -144,7 +146,7 @@ export function shallowCopy(base: any, strict: boolean) {
if (Array.isArray(base)) return Array.prototype.slice.call(base)

if (!strict && isPlainObject(base)) {
if (!Object.getPrototypeOf(base)) {
if (!getPrototypeOf(base)) {
const obj = Object.create(null)
return Object.assign(obj, base)
}
Expand Down Expand Up @@ -172,7 +174,7 @@ export function shallowCopy(base: any, strict: boolean) {
value: base[key]
}
}
return Object.create(Object.getPrototypeOf(base), descriptors)
return Object.create(getPrototypeOf(base), descriptors)
}

/**
Expand Down

0 comments on commit d897c6b

Please sign in to comment.