Skip to content

Commit

Permalink
⚡️ Assign handler in if statement
Browse files Browse the repository at this point in the history
Improves benchmarks to:

```
benchRfdc*100: 203.999ms
benchRfdcProto*100: 215.779ms
benchRfdcCircles*100: 224.12ms
benchRfdcCirclesProto*100: 243.172ms
```
  • Loading branch information
alecgibson committed Jun 12, 2024
1 parent 63b4d45 commit f08c7ec
Showing 1 changed file with 23 additions and 20 deletions.
43 changes: 23 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ function rfdc (opts) {
[Set, (o, fn) => new Set(cloneArray(Array.from(o), fn))]
].concat(opts.constructorHandlers || []))

let handler = null

return opts.proto ? cloneProto : clone

function cloneArray (a, fn) {
Expand All @@ -29,8 +31,8 @@ function rfdc (opts) {
const cur = a[k]
if (typeof cur !== 'object' || cur === null) {
a2[k] = cur
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
a2[k] = constructorHandlers.get(cur.constructor)(cur, fn)
} else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) {
a2[k] = handler(cur, fn)
} else if (ArrayBuffer.isView(cur)) {
a2[k] = copyBuffer(cur)
} else {
Expand All @@ -43,17 +45,17 @@ function rfdc (opts) {
function clone (o) {
if (typeof o !== 'object' || o === null) return o
if (Array.isArray(o)) return cloneArray(o, clone)
if (o.constructor !== Object && constructorHandlers.has(o.constructor)) {
return constructorHandlers.get(o.constructor)(o, clone)
if (o.constructor !== Object && (handler = constructorHandlers.get(o.constructor))) {
return handler(o, clone)
}
const o2 = {}
for (const k in o) {
if (Object.hasOwnProperty.call(o, k) === false) continue
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
o2[k] = constructorHandlers.get(cur.constructor)(cur, clone)
} else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) {
o2[k] = handler(cur, clone)
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
} else {
Expand All @@ -66,16 +68,16 @@ function rfdc (opts) {
function cloneProto (o) {
if (typeof o !== 'object' || o === null) return o
if (Array.isArray(o)) return cloneArray(o, cloneProto)
if (o.constructor !== Object && constructorHandlers.has(o.constructor)) {
return constructorHandlers.get(o.constructor)(o, cloneProto)
if (o.constructor !== Object && (handler = constructorHandlers.get(o.constructor))) {
return handler(o, cloneProto)
}
const o2 = {}
for (const k in o) {
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
o2[k] = constructorHandlers.get(cur.constructor)(cur, cloneProto)
} else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) {
o2[k] = handler(cur, cloneProto)
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
} else {
Expand All @@ -96,6 +98,7 @@ function rfdcCircles (opts) {
[Set, (o, fn) => new Set(cloneArray(Array.from(o), fn))]
].concat(opts.constructorHandlers || []))

let handler = null
return opts.proto ? cloneProto : clone

function cloneArray (a, fn) {
Expand All @@ -106,8 +109,8 @@ function rfdcCircles (opts) {
const cur = a[k]
if (typeof cur !== 'object' || cur === null) {
a2[k] = cur
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
a2[k] = constructorHandlers.get(cur.constructor)(cur, fn)
} else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) {
a2[k] = handler(cur, fn)
} else if (ArrayBuffer.isView(cur)) {
a2[k] = copyBuffer(cur)
} else {
Expand All @@ -125,8 +128,8 @@ function rfdcCircles (opts) {
function clone (o) {
if (typeof o !== 'object' || o === null) return o
if (Array.isArray(o)) return cloneArray(o, clone)
if (o.constructor !== Object && constructorHandlers.has(o.constructor)) {
return constructorHandlers.get(o.constructor)(o, clone)
if (o.constructor !== Object && (handler = constructorHandlers.get(o.constructor))) {
return handler(o, clone)
}
const o2 = {}
refs.push(o)
Expand All @@ -136,8 +139,8 @@ function rfdcCircles (opts) {
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
o2[k] = constructorHandlers.get(cur.constructor)(cur, clone)
} else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) {
o2[k] = handler(cur, clone)
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
} else {
Expand All @@ -157,8 +160,8 @@ function rfdcCircles (opts) {
function cloneProto (o) {
if (typeof o !== 'object' || o === null) return o
if (Array.isArray(o)) return cloneArray(o, cloneProto)
if (o.constructor !== Object && constructorHandlers.has(o.constructor)) {
return constructorHandlers.get(o.constructor)(o, cloneProto)
if (o.constructor !== Object && (handler = constructorHandlers.get(o.constructor))) {
return handler(o, cloneProto)
}
const o2 = {}
refs.push(o)
Expand All @@ -167,8 +170,8 @@ function rfdcCircles (opts) {
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (cur.constructor !== Object && constructorHandlers.has(cur.constructor)) {
o2[k] = constructorHandlers.get(cur.constructor)(cur, cloneProto)
} else if (cur.constructor !== Object && (handler = constructorHandlers.get(cur.constructor))) {
o2[k] = handler(cur, cloneProto)
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
} else {
Expand Down

0 comments on commit f08c7ec

Please sign in to comment.