From f08c7ec79647dd8787ffa8c418befb4f4dc1151a Mon Sep 17 00:00:00 2001 From: Alec Gibson <12036746+alecgibson@users.noreply.github.com> Date: Wed, 12 Jun 2024 09:44:56 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Assign=20handler=20in=20`i?= =?UTF-8?q?f`=20statement?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Improves benchmarks to: ``` benchRfdc*100: 203.999ms benchRfdcProto*100: 215.779ms benchRfdcCircles*100: 224.12ms benchRfdcCirclesProto*100: 243.172ms ``` --- index.js | 43 +++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 20 deletions(-) diff --git a/index.js b/index.js index 9481391..f7d58cd 100644 --- a/index.js +++ b/index.js @@ -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) { @@ -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 { @@ -43,8 +45,8 @@ 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) { @@ -52,8 +54,8 @@ function rfdc (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 { @@ -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 { @@ -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) { @@ -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 { @@ -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) @@ -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 { @@ -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) @@ -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 {