Skip to content

Commit

Permalink
⚡️ Avoid extra array allocation
Browse files Browse the repository at this point in the history
Updated benchmark

```
benchRfdc*100: 206.686ms
benchRfdcProto*100: 218.722ms
benchRfdcCircles*100: 228.578ms
benchRfdcCirclesProto*100: 239.244ms
```
  • Loading branch information
alecgibson committed Jun 12, 2024
1 parent 075a0cc commit b4c0af0
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b4c0af0

Please sign in to comment.