Skip to content

Commit

Permalink
updated benchmarks
Browse files Browse the repository at this point in the history
Signed-off-by: Matteo Collina <hello@matteocollina.com>
  • Loading branch information
mcollina committed Mar 19, 2024
1 parent 60d59a2 commit eb9cfdc
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 71 deletions.
38 changes: 19 additions & 19 deletions benchmark/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,96 +11,96 @@ const plainObjectClone = require('plain-object-clone')
const nanoCopy = require('nano-copy')
const ramdaClone = require('ramda').clone
const cloneDefaults = clone()
const cloneProto = clone({proto: true})
const cloneCircles = clone({circles: true})
const cloneCirclesProto = clone({circles: true, proto: true})
const cloneProto = clone({ proto: true })
const cloneCircles = clone({ circles: true })
const cloneCirclesProto = clone({ circles: true, proto: true })
const max = 1000

var run = bench([
const run = bench([
function benchDeepCopy (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
deepCopy(obj)
}
setImmediate(cb)
},
function benchLodashCloneDeep (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
lodashCloneDeep(obj)
}
setImmediate(cb)
},
function benchCloneDeep (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
cloneDeep(obj)
}
setImmediate(cb)
},
function benchFastCopy (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
fastCopy(obj)
}
setImmediate(cb)
},
function benchFastestJsonCopy (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
copyFastestJsonCopy(obj)
}
setImmediate(cb)
},
function benchPlainObjectClone (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
plainObjectClone(obj)
}
setImmediate(cb)
},
function benchNanoCopy (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
nanoCopy(obj)
}
setImmediate(cb)
},
function benchRamdaClone (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
ramdaClone(obj)
}
setImmediate(cb)
},
function benchJsonParseJsonStringify (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
JSON.parse(JSON.stringify(obj))
}
setImmediate(cb)
},
function benchRfdc (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
cloneDefaults(obj)
}
setImmediate(cb)
},
function benchRfdcProto (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
cloneProto(obj)
}
setImmediate(cb)
},
function benchRfdcCircles (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
cloneCircles(obj)
}
setImmediate(cb)
},
function benchRfdcCirclesProto (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
cloneCirclesProto(obj)
}
setImmediate(cb)
},
function benchStructuredClone (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
structuredClone(obj)
}
setImmediate(cb)
},
}
], 100)

run(run)
42 changes: 21 additions & 21 deletions benchmark/shallow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,99 +4,99 @@ const deepCopy = require('deep-copy')
const lodashCloneDeep = require('lodash.clonedeep')
const cloneDeep = require('clone-deep')
const fastCopy = require('fast-copy').default
const obj = {a: 'a', b: 'b', c: 'c'}
const obj = { a: 'a', b: 'b', c: 'c' }
const clone = require('..')
const copyFastestJsonCopy = require('fastest-json-copy').copy
const plainObjectClone = require('plain-object-clone')
const nanoCopy = require('nano-copy')
const ramdaClone = require('ramda').clone
const cloneDefaults = clone()
const cloneProto = clone({proto: true})
const cloneCircles = clone({circles: true})
const cloneCirclesProto = clone({circles: true, proto: true})
const cloneProto = clone({ proto: true })
const cloneCircles = clone({ circles: true })
const cloneCirclesProto = clone({ circles: true, proto: true })
const max = 1000
/* eslint no-unused-vars: ["error", { "varsIgnorePattern": "^x$" }] */
var run = bench([
const run = bench([
function benchDeepCopy (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
deepCopy(obj)
}
setImmediate(cb)
},
function benchLodashCloneDeep (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
lodashCloneDeep(obj)
}
setImmediate(cb)
},
function benchCloneDeep (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
cloneDeep(obj)
}
setImmediate(cb)
},
function benchFastCopy (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
fastCopy(obj)
}
setImmediate(cb)
},
function benchFastestJsonCopy (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
copyFastestJsonCopy(obj)
}
setImmediate(cb)
},
function benchPlainObjectClone (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
plainObjectClone(obj)
}
setImmediate(cb)
},
function benchNanoCopy (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
nanoCopy(obj)
}
setImmediate(cb)
},
function benchRamdaClone (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
ramdaClone(obj)
}
setImmediate(cb)
},
function benchObjectAssign (cb) {
for (var i = 0; i < max; i++) {
var x = Object.assign({}, obj)
for (let i = 0; i < max; i++) {
const x = Object.assign({}, obj)
}
setImmediate(cb)
},
function benchObjectSpread (cb) {
for (var i = 0; i < max; i++) {
var x = {...obj}
for (let i = 0; i < max; i++) {
const x = { ...obj }
}
setImmediate(cb)
},
function benchRfdc (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
cloneDefaults(obj)
}
setImmediate(cb)
},
function benchRfdcProto (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
cloneProto(obj)
}
setImmediate(cb)
},
function benchRfdcCircles (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
cloneCircles(obj)
}
setImmediate(cb)
},
function benchRfdcCirclesProto (cb) {
for (var i = 0; i < max; i++) {
for (let i = 0; i < max; i++) {
cloneCirclesProto(obj)
}
setImmediate(cb)
Expand Down
54 changes: 27 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ function rfdc (opts) {
return opts.proto ? cloneProto : clone

function cloneArray (a, fn) {
var keys = Object.keys(a)
var a2 = new Array(keys.length)
for (var i = 0; i < keys.length; i++) {
var k = keys[i]
var cur = a[k]
const keys = Object.keys(a)
const a2 = new Array(keys.length)
for (let i = 0; i < keys.length; i++) {
const k = keys[i]
const cur = a[k]
if (typeof cur !== 'object' || cur === null) {
a2[k] = cur
} else if (cur instanceof Date) {
Expand All @@ -40,10 +40,10 @@ function rfdc (opts) {
if (Array.isArray(o)) return cloneArray(o, clone)
if (o instanceof Map) return new Map(cloneArray(Array.from(o), clone))
if (o instanceof Set) return new Set(cloneArray(Array.from(o), clone))
var o2 = {}
for (var k in o) {
const o2 = {}
for (const k in o) {
if (Object.hasOwnProperty.call(o, k) === false) continue
var cur = o[k]
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (cur instanceof Date) {
Expand All @@ -67,9 +67,9 @@ function rfdc (opts) {
if (Array.isArray(o)) return cloneArray(o, cloneProto)
if (o instanceof Map) return new Map(cloneArray(Array.from(o), cloneProto))
if (o instanceof Set) return new Set(cloneArray(Array.from(o), cloneProto))
var o2 = {}
for (var k in o) {
var cur = o[k]
const o2 = {}
for (const k in o) {
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (cur instanceof Date) {
Expand All @@ -89,25 +89,25 @@ function rfdc (opts) {
}

function rfdcCircles (opts) {
var refs = []
var refsNew = []
const refs = []
const refsNew = []

return opts.proto ? cloneProto : clone

function cloneArray (a, fn) {
var keys = Object.keys(a)
var a2 = new Array(keys.length)
for (var i = 0; i < keys.length; i++) {
var k = keys[i]
var cur = a[k]
const keys = Object.keys(a)
const a2 = new Array(keys.length)
for (let i = 0; i < keys.length; i++) {
const k = keys[i]
const cur = a[k]
if (typeof cur !== 'object' || cur === null) {
a2[k] = cur
} else if (cur instanceof Date) {
a2[k] = new Date(cur)
} else if (ArrayBuffer.isView(cur)) {
a2[k] = copyBuffer(cur)
} else {
var index = refs.indexOf(cur)
const index = refs.indexOf(cur)
if (index !== -1) {
a2[k] = refsNew[index]
} else {
Expand All @@ -124,12 +124,12 @@ function rfdcCircles (opts) {
if (Array.isArray(o)) return cloneArray(o, clone)
if (o instanceof Map) return new Map(cloneArray(Array.from(o), clone))
if (o instanceof Set) return new Set(cloneArray(Array.from(o), clone))
var o2 = {}
const o2 = {}
refs.push(o)
refsNew.push(o2)
for (var k in o) {
for (const k in o) {
if (Object.hasOwnProperty.call(o, k) === false) continue
var cur = o[k]
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (cur instanceof Date) {
Expand All @@ -141,7 +141,7 @@ function rfdcCircles (opts) {
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
} else {
var i = refs.indexOf(cur)
const i = refs.indexOf(cur)
if (i !== -1) {
o2[k] = refsNew[i]
} else {
Expand All @@ -160,11 +160,11 @@ function rfdcCircles (opts) {
if (Array.isArray(o)) return cloneArray(o, cloneProto)
if (o instanceof Map) return new Map(cloneArray(Array.from(o), cloneProto))
if (o instanceof Set) return new Set(cloneArray(Array.from(o), cloneProto))
var o2 = {}
const o2 = {}
refs.push(o)
refsNew.push(o2)
for (var k in o) {
var cur = o[k]
for (const k in o) {
const cur = o[k]
if (typeof cur !== 'object' || cur === null) {
o2[k] = cur
} else if (cur instanceof Date) {
Expand All @@ -176,7 +176,7 @@ function rfdcCircles (opts) {
} else if (ArrayBuffer.isView(cur)) {
o2[k] = copyBuffer(cur)
} else {
var i = refs.indexOf(cur)
const i = refs.indexOf(cur)
if (i !== -1) {
o2[k] = refsNew[i]
} else {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"nano-copy": "^0.1.0",
"plain-object-clone": "^1.1.0",
"ramda": "^0.27.1",
"standard": "^11.0.1",
"standard": "^17.0.0",
"tap": "^12.0.1",
"tsd": "^0.7.4"
},
Expand Down
6 changes: 3 additions & 3 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ function types (clone, label) {
})
test(`${label} copies TypedArrays from object correctly`, async ({ ok, is, isNot }) => {
const [input1, input2] = [rnd(10), rnd(10)]
var buffer = new ArrayBuffer(8)
const buffer = new ArrayBuffer(8)
const int32View = new Int32Array(buffer)
int32View[0] = input1
int32View[1] = input2
Expand All @@ -235,7 +235,7 @@ function types (clone, label) {
})
test(`${label} copies TypedArrays from array correctly`, async ({ ok, is, isNot }) => {
const [input1, input2] = [rnd(10), rnd(10)]
var buffer = new ArrayBuffer(16)
const buffer = new ArrayBuffer(16)
const int32View = new Int32Array(buffer)
int32View[0] = input1
int32View[1] = input2
Expand All @@ -247,7 +247,7 @@ function types (clone, label) {
})
test(`${label} copies complex TypedArrays`, async ({ ok, deepEqual, is, isNot }) => {
const [input1, input2, input3] = [rnd(10), rnd(10), rnd(10)]
var buffer = new ArrayBuffer(4)
const buffer = new ArrayBuffer(4)
const view1 = new Int8Array(buffer, 0, 2)
const view2 = new Int8Array(buffer, 2, 2)
const view3 = new Int8Array(buffer)
Expand Down

0 comments on commit eb9cfdc

Please sign in to comment.