From b4c0af0075917d2f56a28ba440f1534e817f89be Mon Sep 17 00:00:00 2001 From: Alec Gibson <12036746+alecgibson@users.noreply.github.com> Date: Wed, 12 Jun 2024 10:36:34 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=EF=B8=8F=20Avoid=20extra=20array=20al?= =?UTF-8?q?location?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Updated benchmark ``` benchRfdc*100: 206.686ms benchRfdcProto*100: 218.722ms benchRfdcCircles*100: 228.578ms benchRfdcCirclesProto*100: 239.244ms ``` --- index.js | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index f7d58cd..a964561 100644 --- a/index.js +++ b/index.js @@ -13,11 +13,15 @@ function rfdc (opts) { opts = opts || {} if (opts.circles) return rfdcCircles(opts) - const constructorHandlers = new Map([ - [Date, (o) => new Date(o)], - [Map, (o, fn) => new Map(cloneArray(Array.from(o), fn))], - [Set, (o, fn) => new Set(cloneArray(Array.from(o), fn))] - ].concat(opts.constructorHandlers || [])) + const constructorHandlers = new Map() + constructorHandlers.set(Date, (o) => new Date(o)) + constructorHandlers.set(Map, (o, fn) => new Map(cloneArray(Array.from(o), fn))) + constructorHandlers.set(Set, (o, fn) => new Set(cloneArray(Array.from(o), fn))) + if (opts.constructorHandlers) { + for (const handler of opts.constructorHandlers) { + constructorHandlers.set(handler[0], handler[1]) + } + } let handler = null @@ -92,11 +96,15 @@ function rfdcCircles (opts) { const refs = [] const refsNew = [] - const constructorHandlers = new Map([ - [Date, (o) => new Date(o)], - [Map, (o, fn) => new Map(cloneArray(Array.from(o), fn))], - [Set, (o, fn) => new Set(cloneArray(Array.from(o), fn))] - ].concat(opts.constructorHandlers || [])) + const constructorHandlers = new Map() + constructorHandlers.set(Date, (o) => new Date(o)) + constructorHandlers.set(Map, (o, fn) => new Map(cloneArray(Array.from(o), fn))) + constructorHandlers.set(Set, (o, fn) => new Set(cloneArray(Array.from(o), fn))) + if (opts.constructorHandlers) { + for (const handler of opts.constructorHandlers) { + constructorHandlers.set(handler[0], handler[1]) + } + } let handler = null return opts.proto ? cloneProto : clone