Skip to content

Commit

Permalink
⚡️ Add constructor handler fast path
Browse files Browse the repository at this point in the history
Improves benchmarks to:

```
benchRfdc*100: 212.82ms
benchRfdcProto*100: 229.3ms
benchRfdcCircles*100: 255.588ms
benchRfdcCirclesProto*100: 238.306ms
```
  • Loading branch information
alecgibson committed Jun 12, 2024
1 parent 68a7ef5 commit 63b4d45
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ function rfdc (opts) {
const cur = a[k]
if (typeof cur !== 'object' || cur === null) {
a2[k] = cur
} else if (constructorHandlers.has(cur.constructor)) {
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
a2[k] = constructorHandlers.get(cur.constructor)(cur, fn)
} else if (ArrayBuffer.isView(cur)) {
a2[k] = copyBuffer(cur)
Expand All @@ -43,7 +43,7 @@ function rfdc (opts) {
function clone (o) {
if (typeof o !== 'object' || o === null) return o
if (Array.isArray(o)) return cloneArray(o, clone)
if (constructorHandlers.has(o.constructor)) {
if (o.constructor !== Object && constructorHandlers.has(o.constructor)) {
return constructorHandlers.get(o.constructor)(o, clone)
}
const o2 = {}
Expand All @@ -52,7 +52,7 @@ function rfdc (opts) {
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (constructorHandlers.has(cur.constructor)) {
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
o2[k] = constructorHandlers.get(cur.constructor)(cur, clone)
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
Expand All @@ -66,15 +66,15 @@ function rfdc (opts) {
function cloneProto (o) {
if (typeof o !== 'object' || o === null) return o
if (Array.isArray(o)) return cloneArray(o, cloneProto)
if (constructorHandlers.has(o.constructor)) {
if (o.constructor !== Object && constructorHandlers.has(o.constructor)) {
return constructorHandlers.get(o.constructor)(o, cloneProto)
}
const o2 = {}
for (const k in o) {
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (constructorHandlers.has(cur.constructor)) {
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
o2[k] = constructorHandlers.get(cur.constructor)(cur, cloneProto)
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
Expand Down Expand Up @@ -106,7 +106,7 @@ function rfdcCircles (opts) {
const cur = a[k]
if (typeof cur !== 'object' || cur === null) {
a2[k] = cur
} else if (constructorHandlers.has(cur.constructor)) {
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
a2[k] = constructorHandlers.get(cur.constructor)(cur, fn)
} else if (ArrayBuffer.isView(cur)) {
a2[k] = copyBuffer(cur)
Expand All @@ -125,7 +125,7 @@ function rfdcCircles (opts) {
function clone (o) {
if (typeof o !== 'object' || o === null) return o
if (Array.isArray(o)) return cloneArray(o, clone)
if (constructorHandlers.has(o.constructor)) {
if (o.constructor !== Object && constructorHandlers.has(o.constructor)) {
return constructorHandlers.get(o.constructor)(o, clone)
}
const o2 = {}
Expand All @@ -136,7 +136,7 @@ function rfdcCircles (opts) {
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (constructorHandlers.has(cur.constructor)) {
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
o2[k] = constructorHandlers.get(cur.constructor)(cur, clone)
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
Expand All @@ -157,7 +157,7 @@ function rfdcCircles (opts) {
function cloneProto (o) {
if (typeof o !== 'object' || o === null) return o
if (Array.isArray(o)) return cloneArray(o, cloneProto)
if (constructorHandlers.has(o.constructor)) {
if (o.constructor !== Object && constructorHandlers.has(o.constructor)) {
return constructorHandlers.get(o.constructor)(o, cloneProto)
}
const o2 = {}
Expand All @@ -167,7 +167,7 @@ function rfdcCircles (opts) {
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (constructorHandlers.has(cur.constructor)) {
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
o2[k] = constructorHandlers.get(cur.constructor)(cur, cloneProto)
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
Expand Down

0 comments on commit 63b4d45

Please sign in to comment.