Skip to content

Commit d897c6b

Browse files
committed
Micro-optimize Object.getPrototypeOf uses
1 parent 3ad1df5 commit d897c6b

File tree

3 files changed

+11
-7
lines changed

3 files changed

+11
-7
lines changed

src/core/proxy.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import {
1212
AnyArray,
1313
Objectish,
1414
getCurrentScope,
15+
getPrototypeOf,
1516
DRAFT_STATE,
1617
die,
1718
createProxy,
@@ -200,7 +201,7 @@ export const objectTraps: ProxyHandler<ProxyState> = {
200201
die(11)
201202
},
202203
getPrototypeOf(state) {
203-
return Object.getPrototypeOf(state.base_)
204+
return getPrototypeOf(state.base_)
204205
},
205206
setPrototypeOf() {
206207
die(12)
@@ -259,11 +260,11 @@ function getDescriptorFromProto(
259260
): PropertyDescriptor | undefined {
260261
// 'in' checks proto!
261262
if (!(prop in source)) return undefined
262-
let proto = Object.getPrototypeOf(source)
263+
let proto = getPrototypeOf(source)
263264
while (proto) {
264265
const desc = Object.getOwnPropertyDescriptor(proto, prop)
265266
if (desc) return desc
266-
proto = Object.getPrototypeOf(proto)
267+
proto = getPrototypeOf(proto)
267268
}
268269
return undefined
269270
}

src/plugins/patches.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
each,
1212
has,
1313
getArchtype,
14+
getPrototypeOf,
1415
isSet,
1516
isMap,
1617
loadPlugin,
@@ -296,7 +297,7 @@ export function enablePatches() {
296297
Array.from(obj.entries()).map(([k, v]) => [k, deepClonePatchValue(v)])
297298
)
298299
if (isSet(obj)) return new Set(Array.from(obj).map(deepClonePatchValue))
299-
const cloned = Object.create(Object.getPrototypeOf(obj))
300+
const cloned = Object.create(getPrototypeOf(obj))
300301
for (const key in obj) cloned[key] = deepClonePatchValue(obj[key])
301302
if (has(obj, immerable)) cloned[immerable] = obj[immerable]
302303
return cloned

src/utils/common.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import {
1111
die
1212
} from "../internal"
1313

14+
export const getPrototypeOf = Object.getPrototypeOf
15+
1416
/** Returns true if the given value is an Immer draft */
1517
/*#__PURE__*/
1618
export function isDraft(value: any): boolean {
@@ -35,7 +37,7 @@ const objectCtorString = Object.prototype.constructor.toString()
3537
/*#__PURE__*/
3638
export function isPlainObject(value: any): boolean {
3739
if (!value || typeof value !== "object") return false
38-
const proto = Object.getPrototypeOf(value)
40+
const proto = getPrototypeOf(value)
3941
if (proto === null) {
4042
return true
4143
}
@@ -144,7 +146,7 @@ export function shallowCopy(base: any, strict: boolean) {
144146
if (Array.isArray(base)) return Array.prototype.slice.call(base)
145147

146148
if (!strict && isPlainObject(base)) {
147-
if (!Object.getPrototypeOf(base)) {
149+
if (!getPrototypeOf(base)) {
148150
const obj = Object.create(null)
149151
return Object.assign(obj, base)
150152
}
@@ -172,7 +174,7 @@ export function shallowCopy(base: any, strict: boolean) {
172174
value: base[key]
173175
}
174176
}
175-
return Object.create(Object.getPrototypeOf(base), descriptors)
177+
return Object.create(getPrototypeOf(base), descriptors)
176178
}
177179

178180
/**

0 commit comments

Comments
 (0)